Skip to content

Commit 0105c26

Browse files
authored
feat: add solutions to lc problem: No.2958 (#2080)
No.2958.Length of Longest Subarray With at Most K Frequency
1 parent 6bede49 commit 0105c26

File tree

7 files changed

+213
-6
lines changed

7 files changed

+213
-6
lines changed

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

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,106 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:双指针**
63+
64+
我们可以用两个指针 $j$ 和 $i$ 分别表示子数组的左右端点,初始时两个指针都指向数组的第一个元素。
65+
66+
接下来,我们遍历数组 $nums$ 中的每个元素 $x$,对于每个元素 $x$,我们将 $x$ 的出现次数加一,然后判断当前子数组是否满足要求。如果当前子数组不满足要求,我们就将指针 $j$ 右移一位,并将 $nums[j]$ 的出现次数减一,直到当前子数组满足要求为止。然后我们更新答案 $ans = \max(ans, i - j + 1)$。继续遍历,直到 $i$ 到达数组的末尾。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
69+
6270
<!-- tabs:start -->
6371

6472
### **Python3**
6573

6674
<!-- 这里可写当前语言的特殊实现逻辑 -->
6775

6876
```python
69-
77+
class Solution:
78+
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
79+
cnt = defaultdict(int)
80+
ans = j = 0
81+
for i, x in enumerate(nums):
82+
cnt[x] += 1
83+
while cnt[x] > k:
84+
cnt[nums[j]] -= 1
85+
j += 1
86+
ans = max(ans, i - j + 1)
87+
return ans
7088
```
7189

7290
### **Java**
7391

7492
<!-- 这里可写当前语言的特殊实现逻辑 -->
7593

7694
```java
77-
95+
class Solution {
96+
public int maxSubarrayLength(int[] nums, int k) {
97+
Map<Integer, Integer> cnt = new HashMap<>();
98+
int ans = 0;
99+
for (int i = 0, j = 0; i < nums.length; ++i) {
100+
cnt.merge(nums[i], 1, Integer::sum);
101+
while (cnt.get(nums[i]) > k) {
102+
cnt.merge(nums[j++], -1, Integer::sum);
103+
}
104+
ans = Math.max(ans, i - j + 1);
105+
}
106+
return ans;
107+
}
108+
}
78109
```
79110

80111
### **C++**
81112

82113
```cpp
83-
114+
class Solution {
115+
public:
116+
int maxSubarrayLength(vector<int>& nums, int k) {
117+
unordered_map<int, int> cnt;
118+
int ans = 0;
119+
for (int i = 0, j = 0; i < nums.size(); ++i) {
120+
++cnt[nums[i]];
121+
while (cnt[nums[i]] > k) {
122+
--cnt[nums[j++]];
123+
}
124+
ans = max(ans, i - j + 1);
125+
}
126+
return ans;
127+
}
128+
};
84129
```
85130
86131
### **Go**
87132
88133
```go
134+
func maxSubarrayLength(nums []int, k int) (ans int) {
135+
cnt := map[int]int{}
136+
for i, j, n := 0, 0, len(nums); i < n; i++ {
137+
cnt[nums[i]]++
138+
for ; cnt[nums[i]] > k; j++ {
139+
cnt[nums[j]]--
140+
}
141+
ans = max(ans, i-j+1)
142+
}
143+
return
144+
}
145+
```
89146

147+
### **TypeScript**
148+
149+
```ts
150+
function maxSubarrayLength(nums: number[], k: number): number {
151+
const cnt: Map<number, number> = new Map();
152+
let ans = 0;
153+
for (let i = 0, j = 0; i < nums.length; ++i) {
154+
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
155+
for (; cnt.get(nums[i])! > k; ++j) {
156+
cnt.set(nums[j], cnt.get(nums[j])! - 1);
157+
}
158+
ans = Math.max(ans, i - j + 1);
159+
}
160+
return ans;
161+
}
90162
```
91163

92164
### **...**

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

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,102 @@ It can be shown that there are no good subarrays with length more than 4.
5353

5454
## Solutions
5555

56+
**Solution 1: Two Pointers**
57+
58+
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.
59+
60+
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.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
```python
61-
69+
class Solution:
70+
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
71+
cnt = defaultdict(int)
72+
ans = j = 0
73+
for i, x in enumerate(nums):
74+
cnt[x] += 1
75+
while cnt[x] > k:
76+
cnt[nums[j]] -= 1
77+
j += 1
78+
ans = max(ans, i - j + 1)
79+
return ans
6280
```
6381

6482
### **Java**
6583

6684
```java
67-
85+
class Solution {
86+
public int maxSubarrayLength(int[] nums, int k) {
87+
Map<Integer, Integer> cnt = new HashMap<>();
88+
int ans = 0;
89+
for (int i = 0, j = 0; i < nums.length; ++i) {
90+
cnt.merge(nums[i], 1, Integer::sum);
91+
while (cnt.get(nums[i]) > k) {
92+
cnt.merge(nums[j++], -1, Integer::sum);
93+
}
94+
ans = Math.max(ans, i - j + 1);
95+
}
96+
return ans;
97+
}
98+
}
6899
```
69100

70101
### **C++**
71102

72103
```cpp
73-
104+
class Solution {
105+
public:
106+
int maxSubarrayLength(vector<int>& nums, int k) {
107+
unordered_map<int, int> cnt;
108+
int ans = 0;
109+
for (int i = 0, j = 0; i < nums.size(); ++i) {
110+
++cnt[nums[i]];
111+
while (cnt[nums[i]] > k) {
112+
--cnt[nums[j++]];
113+
}
114+
ans = max(ans, i - j + 1);
115+
}
116+
return ans;
117+
}
118+
};
74119
```
75120
76121
### **Go**
77122
78123
```go
124+
func maxSubarrayLength(nums []int, k int) (ans int) {
125+
cnt := map[int]int{}
126+
for i, j, n := 0, 0, len(nums); i < n; i++ {
127+
cnt[nums[i]]++
128+
for ; cnt[nums[i]] > k; j++ {
129+
cnt[nums[j]]--
130+
}
131+
ans = max(ans, i-j+1)
132+
}
133+
return
134+
}
135+
```
79136

137+
### **TypeScript**
138+
139+
```ts
140+
function maxSubarrayLength(nums: number[], k: number): number {
141+
const cnt: Map<number, number> = new Map();
142+
let ans = 0;
143+
for (let i = 0, j = 0; i < nums.length; ++i) {
144+
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
145+
for (; cnt.get(nums[i])! > k; ++j) {
146+
cnt.set(nums[j], cnt.get(nums[j])! - 1);
147+
}
148+
ans = Math.max(ans, i - j + 1);
149+
}
150+
return ans;
151+
}
80152
```
81153

82154
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxSubarrayLength(vector<int>& nums, int k) {
4+
unordered_map<int, int> cnt;
5+
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++]];
10+
}
11+
ans = max(ans, i - j + 1);
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
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]]--
7+
}
8+
ans = max(ans, i-j+1)
9+
}
10+
return
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int maxSubarrayLength(int[] nums, int k) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
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);
9+
}
10+
ans = Math.max(ans, i - j + 1);
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
3+
cnt = defaultdict(int)
4+
ans = j = 0
5+
for i, x in enumerate(nums):
6+
cnt[x] += 1
7+
while cnt[x] > k:
8+
cnt[nums[j]] -= 1
9+
j += 1
10+
ans = max(ans, i - j + 1)
11+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxSubarrayLength(nums: number[], k: number): number {
2+
const cnt: Map<number, number> = new Map();
3+
let ans = 0;
4+
for (let i = 0, j = 0; i < nums.length; ++i) {
5+
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
6+
for (; cnt.get(nums[i])! > k; ++j) {
7+
cnt.set(nums[j], cnt.get(nums[j])! - 1);
8+
}
9+
ans = Math.max(ans, i - j + 1);
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)