[Swift]LeetCode1186. 删除一次得到子数组最大和 | Maximum Subarray Sum with One Deletion
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11484995.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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
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 }