Skip to content

Commit 51e41d4

Browse files
authored
feat: update solutions to lc problem: No.2958 (#5348)
1 parent d43e743 commit 51e41d4

8 files changed

Lines changed: 168 additions & 92 deletions

File tree

solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/README.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ tags:
7777

7878
### 方法一:双指针
7979

80-
我们可以用两个指针 $j$ 和 $i$ 分别表示子数组的左右端点,初始时两个指针都指向数组的第一个元素。
80+
我们可以用两个指针 $l$ 和 $r$ 分别表示子数组的左右端点,初始时两个指针都指向数组的第一个元素。
8181

82-
接下来,我们遍历数组 $nums$ 中的每个元素 $x$,对于每个元素 $x$,我们将 $x$ 的出现次数加一,然后判断当前子数组是否满足要求。如果当前子数组不满足要求,我们就将指针 $j$ 右移一位,并将 $nums[j]$ 的出现次数减一,直到当前子数组满足要求为止。然后我们更新答案 $ans = \max(ans, i - j + 1)$。继续遍历,直到 $i$ 到达数组的末尾。
82+
接下来,我们遍历数组 $nums$ 中的每个元素 $x$,对于每个元素 $x$,我们将 $x$ 的出现次数加一,然后判断当前子数组是否满足要求。如果当前子数组不满足要求,我们就将指针 $l$ 右移一位,并将 $nums[l]$ 的出现次数减一,直到当前子数组满足要求为止。然后我们更新答案 $ans = \max(ans, r - l + 1)$。继续遍历,直到 $r$ 到达数组的末尾。
8383

8484
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
8585

@@ -90,14 +90,14 @@ tags:
9090
```python
9191
class Solution:
9292
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
93+
ans = l = 0
9394
cnt = defaultdict(int)
94-
ans = j = 0
95-
for i, x in enumerate(nums):
95+
for r, x in enumerate(nums):
9696
cnt[x] += 1
9797
while cnt[x] > k:
98-
cnt[nums[j]] -= 1
99-
j += 1
100-
ans = max(ans, i - j + 1)
98+
cnt[nums[l]] -= 1
99+
l += 1
100+
ans = max(ans, r - l + 1)
101101
return ans
102102
```
103103

@@ -106,14 +106,14 @@ class Solution:
106106
```java
107107
class Solution {
108108
public int maxSubarrayLength(int[] nums, int k) {
109-
Map<Integer, Integer> cnt = new HashMap<>();
110109
int ans = 0;
111-
for (int i = 0, j = 0; i < nums.length; ++i) {
112-
cnt.merge(nums[i], 1, Integer::sum);
113-
while (cnt.get(nums[i]) > k) {
114-
cnt.merge(nums[j++], -1, Integer::sum);
110+
Map<Integer, Integer> cnt = new HashMap<>();
111+
for (int l = 0, r = 0; r < nums.length; ++r) {
112+
cnt.merge(nums[r], 1, Integer::sum);
113+
while (cnt.get(nums[r]) > k) {
114+
cnt.merge(nums[l++], -1, Integer::sum);
115115
}
116-
ans = Math.max(ans, i - j + 1);
116+
ans = Math.max(ans, r - l + 1);
117117
}
118118
return ans;
119119
}
@@ -126,14 +126,14 @@ class Solution {
126126
class Solution {
127127
public:
128128
int maxSubarrayLength(vector<int>& nums, int k) {
129-
unordered_map<int, int> cnt;
130129
int ans = 0;
131-
for (int i = 0, j = 0; i < nums.size(); ++i) {
132-
++cnt[nums[i]];
133-
while (cnt[nums[i]] > k) {
134-
--cnt[nums[j++]];
130+
unordered_map<int, int> cnt;
131+
for (int l = 0, r = 0; r < nums.size(); ++r) {
132+
++cnt[nums[r]];
133+
while (cnt[nums[r]] > k) {
134+
--cnt[nums[l++]];
135135
}
136-
ans = max(ans, i - j + 1);
136+
ans = max(ans, r - l + 1);
137137
}
138138
return ans;
139139
}
@@ -144,13 +144,14 @@ public:
144144
145145
```go
146146
func maxSubarrayLength(nums []int, k int) (ans int) {
147-
cnt := map[int]int{}
148-
for i, j, n := 0, 0, len(nums); i < n; i++ {
149-
cnt[nums[i]]++
150-
for ; cnt[nums[i]] > k; j++ {
151-
cnt[nums[j]]--
147+
cnt := make(map[int]int)
148+
for l, r := 0, 0; r < len(nums); r++ {
149+
cnt[nums[r]]++
150+
for cnt[nums[r]] > k {
151+
cnt[nums[l]]--
152+
l++
152153
}
153-
ans = max(ans, i-j+1)
154+
ans = max(ans, r-l+1)
154155
}
155156
return
156157
}
@@ -160,19 +161,45 @@ func maxSubarrayLength(nums []int, k int) (ans int) {
160161

161162
```ts
162163
function maxSubarrayLength(nums: number[], k: number): number {
163-
const cnt: Map<number, number> = new Map();
164164
let ans = 0;
165-
for (let i = 0, j = 0; i < nums.length; ++i) {
166-
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
167-
for (; cnt.get(nums[i])! > k; ++j) {
168-
cnt.set(nums[j], cnt.get(nums[j])! - 1);
165+
const cnt = new Map<number, number>();
166+
for (let l = 0, r = 0; r < nums.length; ++r) {
167+
cnt.set(nums[r], (cnt.get(nums[r]) ?? 0) + 1);
168+
while (cnt.get(nums[r])! > k) {
169+
cnt.set(nums[l], cnt.get(nums[l])! - 1);
170+
++l;
169171
}
170-
ans = Math.max(ans, i - j + 1);
172+
ans = Math.max(ans, r - l + 1);
171173
}
172174
return ans;
173175
}
174176
```
175177

178+
#### Rust
179+
180+
```rust
181+
impl Solution {
182+
pub fn max_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
183+
let mut ans = 0;
184+
let mut cnt = std::collections::HashMap::new();
185+
186+
let mut l = 0;
187+
for r in 0..nums.len() {
188+
*cnt.entry(nums[r]).or_insert(0) += 1;
189+
190+
while cnt[&nums[r]] > k {
191+
*cnt.get_mut(&nums[l]).unwrap() -= 1;
192+
l += 1;
193+
}
194+
195+
ans = ans.max((r - l + 1) as i32);
196+
}
197+
198+
ans
199+
}
200+
}
201+
```
202+
176203
<!-- tabs:end -->
177204

178205
<!-- solution:end -->

solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/README_EN.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ It can be shown that there are no good subarrays with length more than 4.
7575

7676
### Solution 1: Two Pointers
7777

78-
We can use two pointers $j$ and $i$ to represent the left and right endpoints of the subarray, initially both pointers point to the first element of the array.
78+
We can use two pointers $l$ and $r$ to represent the left and right endpoints of the subarray, initially both pointers point to the first element of the array.
7979

80-
Next, we iterate over each element $x$ in the array $nums$. For each element $x$, we increment the occurrence count of $x$, then check if the current subarray meets the requirements. If the current subarray does not meet the requirements, we move the pointer $j$ one step to the right, and decrement the occurrence count of $nums[j]$, until the current subarray meets the requirements. Then we update the answer $ans = \max(ans, i - j + 1)$. Continue the iteration until $i$ reaches the end of the array.
80+
Next, we iterate over each element $x$ in the array $nums$. For each element $x$, we increment the occurrence count of $x$, then check if the current subarray meets the requirements. If the current subarray does not meet the requirements, we move the pointer $l$ one step to the right, and decrement the occurrence count of $nums[l]$, until the current subarray meets the requirements. Then we update the answer $ans = \max(ans, r - l + 1)$. Continue the iteration until $r$ reaches the end of the array.
8181

8282
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
8383

@@ -88,14 +88,14 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
8888
```python
8989
class Solution:
9090
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
91+
ans = l = 0
9192
cnt = defaultdict(int)
92-
ans = j = 0
93-
for i, x in enumerate(nums):
93+
for r, x in enumerate(nums):
9494
cnt[x] += 1
9595
while cnt[x] > k:
96-
cnt[nums[j]] -= 1
97-
j += 1
98-
ans = max(ans, i - j + 1)
96+
cnt[nums[l]] -= 1
97+
l += 1
98+
ans = max(ans, r - l + 1)
9999
return ans
100100
```
101101

@@ -104,14 +104,14 @@ class Solution:
104104
```java
105105
class Solution {
106106
public int maxSubarrayLength(int[] nums, int k) {
107-
Map<Integer, Integer> cnt = new HashMap<>();
108107
int ans = 0;
109-
for (int i = 0, j = 0; i < nums.length; ++i) {
110-
cnt.merge(nums[i], 1, Integer::sum);
111-
while (cnt.get(nums[i]) > k) {
112-
cnt.merge(nums[j++], -1, Integer::sum);
108+
Map<Integer, Integer> cnt = new HashMap<>();
109+
for (int l = 0, r = 0; r < nums.length; ++r) {
110+
cnt.merge(nums[r], 1, Integer::sum);
111+
while (cnt.get(nums[r]) > k) {
112+
cnt.merge(nums[l++], -1, Integer::sum);
113113
}
114-
ans = Math.max(ans, i - j + 1);
114+
ans = Math.max(ans, r - l + 1);
115115
}
116116
return ans;
117117
}
@@ -124,14 +124,14 @@ class Solution {
124124
class Solution {
125125
public:
126126
int maxSubarrayLength(vector<int>& nums, int k) {
127-
unordered_map<int, int> cnt;
128127
int ans = 0;
129-
for (int i = 0, j = 0; i < nums.size(); ++i) {
130-
++cnt[nums[i]];
131-
while (cnt[nums[i]] > k) {
132-
--cnt[nums[j++]];
128+
unordered_map<int, int> cnt;
129+
for (int l = 0, r = 0; r < nums.size(); ++r) {
130+
++cnt[nums[r]];
131+
while (cnt[nums[r]] > k) {
132+
--cnt[nums[l++]];
133133
}
134-
ans = max(ans, i - j + 1);
134+
ans = max(ans, r - l + 1);
135135
}
136136
return ans;
137137
}
@@ -142,13 +142,14 @@ public:
142142
143143
```go
144144
func maxSubarrayLength(nums []int, k int) (ans int) {
145-
cnt := map[int]int{}
146-
for i, j, n := 0, 0, len(nums); i < n; i++ {
147-
cnt[nums[i]]++
148-
for ; cnt[nums[i]] > k; j++ {
149-
cnt[nums[j]]--
145+
cnt := make(map[int]int)
146+
for l, r := 0, 0; r < len(nums); r++ {
147+
cnt[nums[r]]++
148+
for cnt[nums[r]] > k {
149+
cnt[nums[l]]--
150+
l++
150151
}
151-
ans = max(ans, i-j+1)
152+
ans = max(ans, r-l+1)
152153
}
153154
return
154155
}
@@ -158,19 +159,45 @@ func maxSubarrayLength(nums []int, k int) (ans int) {
158159

159160
```ts
160161
function maxSubarrayLength(nums: number[], k: number): number {
161-
const cnt: Map<number, number> = new Map();
162162
let ans = 0;
163-
for (let i = 0, j = 0; i < nums.length; ++i) {
164-
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
165-
for (; cnt.get(nums[i])! > k; ++j) {
166-
cnt.set(nums[j], cnt.get(nums[j])! - 1);
163+
const cnt = new Map<number, number>();
164+
for (let l = 0, r = 0; r < nums.length; ++r) {
165+
cnt.set(nums[r], (cnt.get(nums[r]) ?? 0) + 1);
166+
while (cnt.get(nums[r])! > k) {
167+
cnt.set(nums[l], cnt.get(nums[l])! - 1);
168+
++l;
167169
}
168-
ans = Math.max(ans, i - j + 1);
170+
ans = Math.max(ans, r - l + 1);
169171
}
170172
return ans;
171173
}
172174
```
173175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn max_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
181+
let mut ans = 0;
182+
let mut cnt = std::collections::HashMap::new();
183+
184+
let mut l = 0;
185+
for r in 0..nums.len() {
186+
*cnt.entry(nums[r]).or_insert(0) += 1;
187+
188+
while cnt[&nums[r]] > k {
189+
*cnt.get_mut(&nums[l]).unwrap() -= 1;
190+
l += 1;
191+
}
192+
193+
ans = ans.max((r - l + 1) as i32);
194+
}
195+
196+
ans
197+
}
198+
}
199+
```
200+
174201
<!-- tabs:end -->
175202

176203
<!-- solution:end -->

solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public:
33
int maxSubarrayLength(vector<int>& nums, int k) {
4-
unordered_map<int, int> cnt;
54
int ans = 0;
6-
for (int i = 0, j = 0; i < nums.size(); ++i) {
7-
++cnt[nums[i]];
8-
while (cnt[nums[i]] > k) {
9-
--cnt[nums[j++]];
5+
unordered_map<int, int> cnt;
6+
for (int l = 0, r = 0; r < nums.size(); ++r) {
7+
++cnt[nums[r]];
8+
while (cnt[nums[r]] > k) {
9+
--cnt[nums[l++]];
1010
}
11-
ans = max(ans, i - j + 1);
11+
ans = max(ans, r - l + 1);
1212
}
1313
return ans;
1414
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
func maxSubarrayLength(nums []int, k int) (ans int) {
2-
cnt := map[int]int{}
3-
for i, j, n := 0, 0, len(nums); i < n; i++ {
4-
cnt[nums[i]]++
5-
for ; cnt[nums[i]] > k; j++ {
6-
cnt[nums[j]]--
2+
cnt := make(map[int]int)
3+
for l, r := 0, 0; r < len(nums); r++ {
4+
cnt[nums[r]]++
5+
for cnt[nums[r]] > k {
6+
cnt[nums[l]]--
7+
l++
78
}
8-
ans = max(ans, i-j+1)
9+
ans = max(ans, r-l+1)
910
}
1011
return
11-
}
12+
}

solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int maxSubarrayLength(int[] nums, int k) {
3-
Map<Integer, Integer> cnt = new HashMap<>();
43
int ans = 0;
5-
for (int i = 0, j = 0; i < nums.length; ++i) {
6-
cnt.merge(nums[i], 1, Integer::sum);
7-
while (cnt.get(nums[i]) > k) {
8-
cnt.merge(nums[j++], -1, Integer::sum);
4+
Map<Integer, Integer> cnt = new HashMap<>();
5+
for (int l = 0, r = 0; r < nums.length; ++r) {
6+
cnt.merge(nums[r], 1, Integer::sum);
7+
while (cnt.get(nums[r]) > k) {
8+
cnt.merge(nums[l++], -1, Integer::sum);
99
}
10-
ans = Math.max(ans, i - j + 1);
10+
ans = Math.max(ans, r - l + 1);
1111
}
1212
return ans;
1313
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
3+
ans = l = 0
34
cnt = defaultdict(int)
4-
ans = j = 0
5-
for i, x in enumerate(nums):
5+
for r, x in enumerate(nums):
66
cnt[x] += 1
77
while cnt[x] > k:
8-
cnt[nums[j]] -= 1
9-
j += 1
10-
ans = max(ans, i - j + 1)
8+
cnt[nums[l]] -= 1
9+
l += 1
10+
ans = max(ans, r - l + 1)
1111
return ans
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn max_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
3+
let mut ans = 0;
4+
let mut cnt = std::collections::HashMap::new();
5+
6+
let mut l = 0;
7+
for r in 0..nums.len() {
8+
*cnt.entry(nums[r]).or_insert(0) += 1;
9+
10+
while cnt[&nums[r]] > k {
11+
*cnt.get_mut(&nums[l]).unwrap() -= 1;
12+
l += 1;
13+
}
14+
15+
ans = ans.max((r - l + 1) as i32);
16+
}
17+
18+
ans
19+
}
20+
}

0 commit comments

Comments
 (0)