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

[Swift]LeetCode1186.删除一次得到子数组最大和|MaximumSubarraySumwithOneDeletion ...

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

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

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

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

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

Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.

Note that the subarray needs to be non-empty after deleting one element.

 

Example 1:

Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.

Example 2:

Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.

Example 3:

Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.

 

Constraints:

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

给你一个整数数组,返回它的某个 非空 子数组(连续元素)在执行一次可选的删除操作后,所能得到的最大元素总和。

换句话说,你可以从原数组中选出一个子数组,并可以决定要不要从中删除一个元素(只能删一次哦),(删除后)子数组中至少应当有一个元素,然后该子数组(剩下)的元素总和是所有子数组之中最大的。

注意,删除一个元素后,子数组 不能为空。

请看示例:

示例 1:

输入:arr = [1,-2,0,3]
输出:4
解释:我们可以选出 [1, -2, 0, 3],然后删掉 -2,这样得到 [1, 0, 3],和最大。

示例 2:

输入:arr = [1,-2,-2,3]
输出:3
解释:我们直接选出 [3],这就是最大和。

示例 3:

输入:arr = [-1,-1,-1,-1]
输出:-1
解释:最后得到的子数组不能为空,所以我们不能选择 [-1] 并从中删去 -1 来得到 0。
     我们应该直接选择 [-1],或者选择 [-1, -1] 再从中删去一个 -1。

 

提示:

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

Runtime: 236 ms

Memory Usage: 24.5 MB
 1 class Solution {
 2     func maximumSum(_ arr: [Int]) -> Int {
 3         let n:Int = arr.count
 4         if n == 1
 5         {
 6             return arr.first!
 7         }
 8         var leftMaxSum:[Int] = [Int](repeating:0,count:n)
 9         for i in 1..<n
10         {
11             leftMaxSum[i] = max(arr[i - 1], leftMaxSum[i - 1] + arr[i - 1])
12         }
13         var rightMaxSum:[Int] = [Int](repeating:0,count:n)
14         for i in stride(from:n - 2,through:0,by:-1)
15         {
16             rightMaxSum[i] = max(arr[i + 1], rightMaxSum[i + 1] + arr[i + 1])
17         }
18         var maxNum:Int = max(leftMaxSum[n - 1], rightMaxSum[0])
19         for i in 1..<(n - 1)
20         {
21             maxNum = max(maxNum, leftMaxSum[i])
22             maxNum = max(maxNum, rightMaxSum[i])
23             maxNum = max(maxNum, leftMaxSum[i] + rightMaxSum[i])
24         }
25         return maxNum
26     }
27 }

 244ms

 1 class Solution {
 2     func maximumSum(_ arr: [Int]) -> Int {
 3         if arr.isEmpty { return 0 }
 4     if arr.count == 1 { return arr[0] }
 5         var forward = Array(repeating: 0, count: arr.count)
 6     var backward = Array(repeating: 0, count: arr.count)
 7     var currMax = arr[0], overMax = arr[0], answer = 0
 8     forward[0] = arr[0]
 9     for i in 1..<arr.count {
10         currMax = max(arr[i], currMax + arr[i])
11         overMax = max(overMax, currMax)
12         forward[i] = currMax
13     }
14     currMax = arr[arr.count-1]
15     overMax = arr[arr.count-1]
16     backward[arr.count-1] = arr[arr.count-1]
17     for i in (0..<arr.count-1).reversed() {
18         currMax = max(arr[i], currMax + arr[i])
19         overMax = max(overMax, currMax)
20         backward[i] = currMax
21     }
22     answer = overMax
23     for i in 1..<arr.count-1 {
24         answer = max(answer, forward[i-1]+backward[i+1])
25     }
26     return answer
27     }
28 }

248ms

 1 class Solution {
 2     func maximumSum(_ arr: [Int]) -> Int {
 3                 
 4         if arr.count == 0 {
 5             return -1
 6         }
 7         if arr.count == 1 {
 8             return arr[0]
 9         }
10 
11         var dp = [Int](repeating: 0, count: arr.count)
12         dp[0] = arr[0]
13         var max1 = dp[0]
14         for i in 1..<arr.count {
15             if dp[i-1] >= 0 {
16                 dp[i] = arr[i] + dp[i-1]
17             } else {
18                 dp[i] = arr[i]
19             }
20             max1 = max(max1, dp[i])
21         }
22 
23         var dp2 = [Int](repeating: 0, count: arr.count)
24         dp2[arr.count-1] = arr[arr.count-1]
25         var max2 = dp2[arr.count-1]
26         for i in stride(from: arr.count-2, to: -1, by: -1) {
27             if dp2[i+1] > 0 {
28                 dp2[i] = arr[i] + dp2[i+1]
29             } else {
30                 dp2[i] = arr[i]
31             }
32             max2 = max(dp2[i], max2)
33         }
34         var max3 = max(max1, max2)
35         
36         for i in 1..<arr.count-1 {
37             max3 = max(dp[i-1]+dp2[i+1], max3)
38         }
39         return max3
40     }
41 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Swift---访问控制(fileprivate,private,internal,public,open)发布时间:2022-07-13
下一篇:
iOS-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