[Swift]LeetCode1005. K 次取反後最大化的數組和 | Maximize Sum Of Array After K Negations

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-eibchrap-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.  (We may choose the same index i multiple times.)git

Return the largest possible sum of the array after modifying it in this way. github

Example 1:數組

Input: A = [4,2,3], K = 1 Output: 5 Explanation: Choose indices (1,) and A becomes [4,-2,3]. 

Example 2:微信

Input: A = [3,-1,0,2], K = 3 Output: 6 Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2]. 

Example 3:this

Input: A = [2,-3,-1,5,-4], K = 2 Output: 13 Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4]. 

Note:spa

  1. 1 <= A.length <= 10000
  2. 1 <= K <= 10000
  3. -100 <= A[i] <= 100

給定一個整數數組 A,咱們只能用如下方法修改該數組:咱們選擇某個個索引 i 並將 A[i] 替換爲 -A[i],而後總共重複這個過程 K 次。(咱們能夠屢次選擇同一個索引 i。)code

以這種方式修改數組後,返回數組可能的最大和。 htm

示例 1:blog

輸入:A = [4,2,3], K = 1
輸出:5
解釋:選擇索引 (1,) ,而後 A 變爲 [4,-2,3]。

示例 2:

輸入:A = [3,-1,0,2], K = 3
輸出:6
解釋:選擇索引 (1, 2, 2) ,而後 A 變爲 [3,1,0,2]。

示例 3:

輸入:A = [2,-3,-1,5,-4], K = 2
輸出:13
解釋:選擇索引 (1, 4) ,而後 A 變爲 [2,3,-1,5,4]。 

提示:

  1. 1 <= A.length <= 10000
  2. 1 <= K <= 10000
  3. -100 <= A[i] <= 100

Runtime: 24 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func largestSumAfterKNegations(_ A: [Int], _ K: Int) -> Int {
 3         var A = A.sorted()
 4         var K = K
 5         var minValue = Int.max
 6         var minIndex = 0
 7         for i in 0..<A.count {
 8             if K > 0 && A[i] < 0
 9             {
10                 A[i] = -A[i]
11                 K -= 1                
12             }
13             if A[i] < minValue {
14                 minValue = A[i]
15                 minIndex = i
16             }
17         }
18         
19         if K % 2 == 1 {
20             A[minIndex] = -A[minIndex]
21         }
22         return A.reduce(0,+)
23     }
24 }

28ms

 1 class Solution {
 2     func largestSumAfterKNegations(_ A: [Int], _ K: Int) -> Int {
 3         var aS = A.sorted()
 4         
 5         for i in 0 ..< A.count {
 6             if i < K && aS[i] <= 0 {
 7                 aS[i] = -aS[i]
 8                 if aS[i] == 0 {
 9                     break
10                 }
11             } else if i < K && aS[i] > 0 {
12                 if i == 0 {
13                     aS[0] = K % 2 == 0 ? aS[0] : -aS[0]
14                     break
15                 } else {
16                     if aS[i - 1] < aS[i] {
17                         aS[i - 1] = (K - i) % 2 == 0 ? aS[i-1] : -aS[i-1]
18                     } else {
19                         aS[i] = (K - i) % 2 == 0 ? aS[i] : -aS[i]
20                     }
21                     break
22                 }
23             }
24         }
25         
26         return aS.reduce(0, { $0 + $1 })
27     }
28 }

32ms

 1 class Solution {
 2     func largestSumAfterKNegations(_ A: [Int], _ K: Int) -> Int {
 3         var A = A
 4         var K = K
 5         A.sort()
 6         for i in 0..<A.count
 7         {
 8             if K > 0 && A[i] < 0
 9             {
10                 A[i] = -A[i]
11                 K -= 1                
12             }
13         }
14         A.sort()
15         if K % 2 == 1
16         {
17             A[0] = -A[0]
18         }
19         return A.reduce(0,+)
20     }
21 }

52ms

 1 class Solution {
 2     func largestSumAfterKNegations(_ A: [Int], _ K: Int) -> Int {
 3         var ACopy = A.sorted()
 4         for _ in 0..<K {
 5             let (minNum, minIndex) = getMinNumAndIndex(ACopy)
 6             ACopy[minIndex] = -minNum
 7         }
 8         return ACopy.reduce(0, +)
 9     }
10     
11     func getMinNumAndIndex(_ A: [Int]) -> (Int, Int) {
12         var minNum = Int.max
13         var minIndex = Int.max
14         for index in 0..<A.count {
15             let num = A[index]
16             if num < minNum {
17                 minNum = num
18                 minIndex = index
19             }
20         }
21         return (minNum, minIndex)
22     }
23 }

72ms

 1 class Solution {
 2     func largestSumAfterKNegations(_ A: [Int], _ K: Int) -> Int {
 3         var AA = A
 4         for _ in 0..<K {
 5             let tuple = findWhatToNegate(AA)
 6             AA[tuple.1] = -tuple.0
 7         }
 8         return AA.reduce(0, +)
 9     }
10     private func findWhatToNegate(_ arr: [Int]) -> (Int, Int) {
11         var smallestPositiveNumber : Int = -1
12         var smallestPositiveNumberIndex : Int = -1
13         var smallestNegativeNumber: Int = 1
14         var smallestNegativeNumberIndex: Int = -1
15 
16         for index in 0..<arr.count {
17             let value = arr[index]
18             if value >= 0 {
19                 if smallestPositiveNumber == -1 {
20                     smallestPositiveNumber = value
21                     smallestPositiveNumberIndex = index
22                 } else if value <= smallestPositiveNumber {
23                     smallestPositiveNumber = value
24                     smallestPositiveNumberIndex = index
25                 }
26             } else {
27                 if smallestNegativeNumber == 1 {
28                     smallestNegativeNumber = value
29                     smallestNegativeNumberIndex = index
30                 } else if value <= smallestNegativeNumber {
31                     smallestNegativeNumber = value
32                     smallestNegativeNumberIndex = index
33                 }
34             }
35         }
36         if smallestNegativeNumber < 1 {
37             return (smallestNegativeNumber, smallestNegativeNumberIndex)
38         } else {
39             return (smallestPositiveNumber, smallestPositiveNumberIndex)
40         }
41     }
42 }
相關文章
相關標籤/搜索