-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2958 (#2080)
No.2958.Length of Longest Subarray With at Most K Frequency
- Loading branch information
Showing
7 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int maxSubarrayLength(vector<int>& nums, int k) { | ||
unordered_map<int, int> cnt; | ||
int ans = 0; | ||
for (int i = 0, j = 0; i < nums.size(); ++i) { | ||
++cnt[nums[i]]; | ||
while (cnt[nums[i]] > k) { | ||
--cnt[nums[j++]]; | ||
} | ||
ans = max(ans, i - j + 1); | ||
} | ||
return ans; | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
func maxSubarrayLength(nums []int, k int) (ans int) { | ||
cnt := map[int]int{} | ||
for i, j, n := 0, 0, len(nums); i < n; i++ { | ||
cnt[nums[i]]++ | ||
for ; cnt[nums[i]] > k; j++ { | ||
cnt[nums[j]]-- | ||
} | ||
ans = max(ans, i-j+1) | ||
} | ||
return | ||
} |
14 changes: 14 additions & 0 deletions
14
solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public int maxSubarrayLength(int[] nums, int k) { | ||
Map<Integer, Integer> cnt = new HashMap<>(); | ||
int ans = 0; | ||
for (int i = 0, j = 0; i < nums.length; ++i) { | ||
cnt.merge(nums[i], 1, Integer::sum); | ||
while (cnt.get(nums[i]) > k) { | ||
cnt.merge(nums[j++], -1, Integer::sum); | ||
} | ||
ans = Math.max(ans, i - j + 1); | ||
} | ||
return ans; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def maxSubarrayLength(self, nums: List[int], k: int) -> int: | ||
cnt = defaultdict(int) | ||
ans = j = 0 | ||
for i, x in enumerate(nums): | ||
cnt[x] += 1 | ||
while cnt[x] > k: | ||
cnt[nums[j]] -= 1 | ||
j += 1 | ||
ans = max(ans, i - j + 1) | ||
return ans |
12 changes: 12 additions & 0 deletions
12
solution/2900-2999/2958.Length of Longest Subarray With at Most K Frequency/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function maxSubarrayLength(nums: number[], k: number): number { | ||
const cnt: Map<number, number> = new Map(); | ||
let ans = 0; | ||
for (let i = 0, j = 0; i < nums.length; ++i) { | ||
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1); | ||
for (; cnt.get(nums[i])! > k; ++j) { | ||
cnt.set(nums[j], cnt.get(nums[j])! - 1); | ||
} | ||
ans = Math.max(ans, i - j + 1); | ||
} | ||
return ans; | ||
} |