-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3026-MaximumGoodSubarraySum.go
More file actions
101 lines (87 loc) · 4.12 KB
/
3026-MaximumGoodSubarraySum.go
File metadata and controls
101 lines (87 loc) · 4.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
// 3026. Maximum Good Subarray Sum
// You are given an array nums of length n and a positive integer k.
// A subarray of nums is called good if the absolute difference between its first and last element is exactly k,
// in other words, the subarray nums[i..j] is good if |nums[i] - nums[j]| == k.
// Return the maximum sum of a good subarray of nums.
// If there are no good subarrays, return 0.
// Example 1:
// Input: nums = [1,2,3,4,5,6], k = 1
// Output: 11
// Explanation: The absolute difference between the first and last element must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].
// Example 2:
// Input: nums = [-1,3,2,4,5], k = 3
// Output: 11
// Explanation: The absolute difference between the first and last element must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].
// Example 3:
// Input: nums = [-1,-2,-3,-4], k = 2
// Output: -6
// Explanation: The absolute difference between the first and last element must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].
// Constraints:
// 2 <= nums.length <= 10^5
// -10^9 <= nums[i] <= 10^9
// 1 <= k <= 10^9
import "fmt"
func maximumSubarraySum(nums []int, k int) int64 {
res, sum, n := int(-1e18), 0, len(nums)
mp := make(map[int]int, n)
for i := 0; i < n; i++ {
sum += nums[i]
if v, ok := mp[nums[i] + k]; ok && res < sum - v {
res = sum - v
}
if v, ok := mp[nums[i] - k]; ok && res < sum - v {
res = sum - v
}
if v, ok := mp[nums[i]]; !ok || (ok && sum - nums[i] < v) {
mp[nums[i]] = sum - nums[i]
}
}
if res == -1e18 { return 0 }
return int64(res)
}
func maximumSubarraySum1(nums []int, k int) int64 {
res, sum := int64(-1 << 63), 0
mp := make(map[int]int)
max := func (x, y int64) int64 { if x > y { return x; }; return y; }
for i := 0; i < len(nums); i++ {
num := nums[i]
if v, ok := mp[num]; !ok || sum < v { // 记录上一步,没有则初始化
mp[num] = sum
}
sum += num
// 找+-k的差值
if v, ok := mp[num - k]; ok {
res = max(res, int64(sum - v))
}
if v, ok := mp[num + k]; ok {
res = max(res, int64(sum - v))
}
}
if res == int64(-1 << 63) { res = 0 }
return res
}
func main() {
// Example 1:
// Input: nums = [1,2,3,4,5,6], k = 1
// Output: 11
// Explanation: The absolute difference between the first and last element must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].
fmt.Println(maximumSubarraySum([]int{1,2,3,4,5,6}, 1)) // 11
// Example 2:
// Input: nums = [-1,3,2,4,5], k = 3
// Output: 11
// Explanation: The absolute difference between the first and last element must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].
fmt.Println(maximumSubarraySum([]int{-1,3,2,4,5}, 3)) // 11
// Example 3:
// Input: nums = [-1,-2,-3,-4], k = 2
// Output: -6
// Explanation: The absolute difference between the first and last element must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].
fmt.Println(maximumSubarraySum([]int{-1,-2,-3,-4}, 2)) // -6
fmt.Println(maximumSubarraySum([]int{1,2,3,4,5,6,7,8,9}, 2)) // 24
fmt.Println(maximumSubarraySum([]int{9,8,7,6,5,4,3,2,1}, 2)) // 24
fmt.Println(maximumSubarraySum1([]int{1,2,3,4,5,6}, 1)) // 11
fmt.Println(maximumSubarraySum1([]int{-1,3,2,4,5}, 3)) // 11
fmt.Println(maximumSubarraySum1([]int{-1,-2,-3,-4}, 2)) // -6
fmt.Println(maximumSubarraySum1([]int{1,2,3,4,5,6,7,8,9}, 2)) // 24
fmt.Println(maximumSubarraySum1([]int{9,8,7,6,5,4,3,2,1}, 2)) // 24
}