• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1005.K次取反后最大化的数组和|MaximizeSumOfArrayAfterKNegations ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 = 1
Output: 5
Explanation: Choose indices (1,) and A becomes [4,-2,3].

Example 2:

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

Example 3:

Input: A = 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift中字符串中含有emoji表情时长度计算不准确的问题发布时间:2022-07-13
下一篇:
swift中标签的使用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap