-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2841-MaximumSumOfAlmostUniqueSubarray.go
More file actions
140 lines (124 loc) · 4.83 KB
/
2841-MaximumSumOfAlmostUniqueSubarray.go
File metadata and controls
140 lines (124 loc) · 4.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
// 2841. Maximum Sum of Almost Unique Subarray
// You are given an integer array nums and two positive integers m and k.
// Return the maximum sum out of all almost unique subarrays of length k of nums.
// If no such subarray exists, return 0.
// A subarray of nums is almost unique if it contains at least m distinct elements.
// A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: nums = [2,6,7,3,1,7], m = 3, k = 4
// Output: 18
// Explanation: There are 3 almost unique subarrays of size k = 4. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18.
// Example 2:
// Input: nums = [5,9,9,2,4,5,4], m = 1, k = 3
// Output: 23
// Explanation: There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23.
// Example 3:
// Input: nums = [1,2,1,2,1,2,1], m = 3, k = 3
// Output: 0
// Explanation: There are no subarrays of size k = 3 that contain at least m = 3 distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0.
// Constraints:
// 1 <= nums.length <= 2 * 10^4
// 1 <= m <= k <= nums.length
// 1 <= nums[i] <= 10^9
import "fmt"
func maxSum(nums []int, m int, k int) int64 {
mp := make(map[int]int)
res, sum := 0, 0
for i := 0; i < k; i++ {
sum += nums[i]
mp[nums[i]]++
}
if len(mp) >= m {
res = sum
}
i, j, n := k, 0, len(nums)
for i < n {
num, prev := nums[i], nums[j]
i++
j++
mp[num]++
mp[prev]--
sum += (num - prev)
if mp[prev] == 0 {
delete(mp, prev)
}
if len(mp) >= m && res < sum {
res = sum
}
}
return int64(res)
}
func maxSum1(nums []int, m int, k int) int64 {
res, sum, n := 0, 0, len(nums)
mp := make(map[int]int, n)
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 0; i < n; i++ {
if i < k - 1 {
mp[nums[i]]++
sum += nums[i]
continue
}
sum += nums[i]
mp[nums[i]]++
if len(mp) >= m {
res = max(res, sum)
}
// 判断是否需要移除 nums[]
sum -= nums[i - k + 1]
if mp[nums[i - k + 1]] == 1 {
delete(mp, nums[i - k + 1])
} else {
mp[nums[i - k + 1]]--
}
}
return int64(res)
}
func maxSum2(nums []int, m int, k int) int64 {
mp := make(map[int]int)
res, sum := 0, 0
for i, v := range nums {
mp[v]++
sum += v
if i < k - 1 { continue }
if len(mp) >= m {
res = max(res, sum)
}
tail := nums[i - k + 1]
sum -= tail
mp[tail]--
if mp[tail] == 0 {
delete(mp, tail)
}
}
return int64(res)
}
func main() {
// Example 1:
// Input: nums = [2,6,7,3,1,7], m = 3, k = 4
// Output: 18
// Explanation: There are 3 almost unique subarrays of size k = 4. These subarrays are [2, 6, 7, 3], [6, 7, 3, 1], and [7, 3, 1, 7]. Among these subarrays, the one with the maximum sum is [2, 6, 7, 3] which has a sum of 18.
fmt.Println(maxSum([]int{2,6,7,3,1,7}, 3, 4)) // 18
// Example 2:
// Input: nums = [5,9,9,2,4,5,4], m = 1, k = 3
// Output: 23
// Explanation: There are 5 almost unique subarrays of size k. These subarrays are [5, 9, 9], [9, 9, 2], [9, 2, 4], [2, 4, 5], and [4, 5, 4]. Among these subarrays, the one with the maximum sum is [5, 9, 9] which has a sum of 23.
fmt.Println(maxSum([]int{5,9,9,2,4,5,4}, 1, 3)) // 23
// Example 3:
// Input: nums = [1,2,1,2,1,2,1], m = 3, k = 3
// Output: 0
// Explanation: There are no subarrays of size k = 3 that contain at least m = 3 distinct elements in the given array [1,2,1,2,1,2,1]. Therefore, no almost unique subarrays exist, and the maximum sum is 0.
fmt.Println(maxSum([]int{1,2,1,2,1,2,1}, 3, 3)) // 0
fmt.Println(maxSum([]int{1,2,3,4,5,6,7,8,9}, 3, 3)) // 24
fmt.Println(maxSum([]int{9,8,7,6,5,4,3,2,1}, 3, 3)) // 24
fmt.Println(maxSum1([]int{2,6,7,3,1,7}, 3, 4)) // 18
fmt.Println(maxSum1([]int{5,9,9,2,4,5,4}, 1, 3)) // 23
fmt.Println(maxSum1([]int{1,2,1,2,1,2,1}, 3, 3)) // 0
fmt.Println(maxSum1([]int{1,2,3,4,5,6,7,8,9}, 3, 3)) // 24
fmt.Println(maxSum1([]int{9,8,7,6,5,4,3,2,1}, 3, 3)) // 24
fmt.Println(maxSum2([]int{2,6,7,3,1,7}, 3, 4)) // 18
fmt.Println(maxSum2([]int{5,9,9,2,4,5,4}, 1, 3)) // 23
fmt.Println(maxSum2([]int{1,2,1,2,1,2,1}, 3, 3)) // 0
fmt.Println(maxSum2([]int{1,2,3,4,5,6,7,8,9}, 3, 3)) // 24
fmt.Println(maxSum2([]int{9,8,7,6,5,4,3,2,1}, 3, 3)) // 24
}