-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2831-FindTheLongestEqualSubarray.go
More file actions
93 lines (84 loc) · 3.29 KB
/
2831-FindTheLongestEqualSubarray.go
File metadata and controls
93 lines (84 loc) · 3.29 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
package main
// 2831. Find the Longest Equal Subarray
// You are given a 0-indexed integer array nums and an integer k.
// A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
// Return the length of the longest possible equal subarray after deleting at most k elements from nums.
// A subarray is a contiguous, possibly empty sequence of elements within an array.
// Example 1:
// Input: nums = [1,3,2,3,1,3], k = 3
// Output: 3
// Explanation: It's optimal to delete the elements at index 2 and index 4.
// After deleting them, nums becomes equal to [1, 3, 3, 3].
// The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
// It can be proven that no longer equal subarrays can be created.
// Example 2:
// Input: nums = [1,1,2,2,1,1], k = 2
// Output: 4
// Explanation: It's optimal to delete the elements at index 2 and index 3.
// After deleting them, nums becomes equal to [1, 1, 1, 1].
// The array itself is an equal subarray, so the answer is 4.
// It can be proven that no longer equal subarrays can be created.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= nums.length
// 0 <= k <= nums.length
import "fmt"
func longestEqualSubarray(nums []int, k int) int {
res, dict := 1, map[int][]int{}
for i, v := range nums {
dict[v] = append(dict[v], i)
}
max := func (x, y int) int { if x > y { return x; }; return y; }
for _, arr := range dict {
k1, l := k, 0
for r := 1; r < len(arr); r++ {
k1 -= arr[r] - arr[r-1] - 1
for k1 < 0 {
k1 += arr[l+1] - arr[l] - 1
l++
}
res = max(res, r - l + 1)
}
}
return res
}
func longestEqualSubarray1(nums []int, k int) int {
res, pos := 0, make([][]int, len(nums) + 1)
for i, x := range nums {
pos[x] = append(pos[x], i - len(pos[x]))
}
max := func (x, y int) int { if x > y { return x; }; return y; }
for _, ps := range pos {
if len(ps) <= res {
continue
}
left := 0
for right, p := range ps {
for p - ps[left] > k { // 要删除的数太多了
left++
}
res = max(res, right-left+1)
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [1,3,2,3,1,3], k = 3
// Output: 3
// Explanation: It's optimal to delete the elements at index 2 and index 4.
// After deleting them, nums becomes equal to [1, 3, 3, 3].
// The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
// It can be proven that no longer equal subarrays can be created.
fmt.Println(longestEqualSubarray([]int{1,3,2,3,1,3}, 3)) // 3
// Example 2:
// Input: nums = [1,1,2,2,1,1], k = 2
// Output: 4
// Explanation: It's optimal to delete the elements at index 2 and index 3.
// After deleting them, nums becomes equal to [1, 1, 1, 1].
// The array itself is an equal subarray, so the answer is 4.
// It can be proven that no longer equal subarrays can be created.
fmt.Println(longestEqualSubarray([]int{1,1,2,2,1,1}, 2)) // 4
fmt.Println(longestEqualSubarray1([]int{1,3,2,3,1,3}, 3)) // 3
fmt.Println(longestEqualSubarray1([]int{1,1,2,2,1,1}, 2)) // 4
}