-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1186-MaximumSubarraySumWithOneDeletion.go
More file actions
74 lines (63 loc) · 2.74 KB
/
1186-MaximumSubarraySumWithOneDeletion.go
File metadata and controls
74 lines (63 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
// 1186. Maximum Subarray Sum with One Deletion
// 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
import "fmt"
func maximumSum(nums []int) int {
res, n := nums[0], len(nums)
dp := make([][2]int, n) // dp[i][0] stores max sum ending at i without deletion, // dp[i][1] stores max sum ending at i with one deletion
dp[0][0], dp[0][1] = nums[0], nums[0]
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 1; i < n; i++ {
dp[i][0] = max(nums[i], dp[i-1][0] + nums[i])
dp[i][1] = max(dp[i-1][0], dp[i-1][1]+ nums[i])
res = max(res, max(dp[i][0], dp[i][1]))
}
return res
}
func maximumSum1(arr []int) int {
dp0, dp1, res := arr[0], 0, arr[0]
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 1; i < len(arr); i++ {
dp0, dp1 = max(dp0, 0) + arr[i], max(dp1 + arr[i], dp0)
res = max(res, max(dp0, dp1))
}
return res
}
func main() {
// 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.
fmt.Println(maximumSum([]int{1,-2,0,3})) // 4
// Example 2:
// Input: arr = [1,-2,-2,3]
// Output: 3
// Explanation: We just choose [3] and it's the maximum sum.
fmt.Println(maximumSum([]int{1,-2,-2,3})) // 3
// 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.
fmt.Println(maximumSum([]int{-1,-1,-1,-1})) // -1
fmt.Println(maximumSum1([]int{1,-2,0,3})) // 4
fmt.Println(maximumSum1([]int{1,-2,-2,3})) // 3
fmt.Println(maximumSum1([]int{-1,-1,-1,-1})) // -1
}