为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10504835.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.)

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

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:

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:

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

给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。)

以这种方式修改数组后,返回数组可能的最大和。 

示例 1:

输入: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 }

 

posted @ 2019-03-10 12:08  为敢技术  阅读(265)  评论(0编辑  收藏  举报