Skip to content

Commit 0186fd6

Browse files
committed
Added 2958, 2962
1 parent 749d09f commit 0186fd6

3 files changed

+62
-1
lines changed

Readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
| 593 | [Valid Square](src/593.valid-square.py) | Medium | O(1) | O(1) | Math | | |
260260
| 605 | [Can Place Flowers](src/605.can-place-flowers.py) | Easy | O(N) | O(1) | Array, Greedy | | |
261261
| 611 | [Valid Triangle Number](src/611.valid-triangle-number.py) | Medium | O(N^2) | O(1) | Array, Two Pointers, Binary Search, Greedy, Sorting | | |
262-
| 621 | [Task Scheduler](src/621.task-scheduler.py) | Medium | O(N) | O(1) | Array, Hash Table, Greedy, Sorting, Heap (Priority Queue), Counting | Greedy | |
262+
| 621 | [Task Scheduler](src/621.task-scheduler.py) | Medium | O(N) | O(1) | Array, Hash Table, Greedy, Sorting, Heap (Priority Queue), Counting | Greedy | |
263263
| 622 | [Design Circular Queue](src/622.design-circular-queue.py) | Medium | O(1) | O(1) | Design, Queue | | |
264264
| 623 | [Add One Row to Tree](src/623.add-one-row-to-tree.py) | Medium | O(N) | O(N) | Tree | | |
265265
| 624 | [Maximum Distance in Arrays](src/624.py) | Easy | O(N) | O(1) | Hash Table, Array, Premium | | 🔒 |
@@ -695,6 +695,8 @@
695695
| 2487 | [Remove Nodes From Linked List](src/2487.remove-nodes-from-linked-list.py) | Medium | O(N) | O(N) | Linked List, Stack, Recursion, Monotonic Stack | | |
696696
| 2498 | [Frog Jump II](src/2498.frog-jump-ii.py) | Medium | O(N) | O(1) | Array, Binary Search, Greedy | | |
697697
| 2517 | [Maximum Tastiness of Candy Basket](src/2517.maximum-tastiness-of-candy-basket.py) | Medium | O(NlogN) | O(1) | Array, Binary Search, Sorting | | |
698+
| 2958 | [Length of Longest Subarray With at Most K Frequency](src/2958.length-of-longest-subarray-with-at-most-k-frequency.py) | Medium | O(N) | O(N) | Array, Hash Table, Sliding Window | | |
699+
| 2962 | [Count Subarrays Where Max Element Appears at Least K Times](src/2962.count-subarrays-where-max-element-appears-at-least-k-times.py) | Medium | O(N) | O(N) | Array, Sliding Window | | |
698700
| 2966 | [Divide Array Into Arrays With Max Difference](src/2966.divide-array-into-arrays-with-max-difference.py) | Medium | O(NlogN) | O(N) | Array, Greedy, Sorting | | |
699701
| 2971 | [Find Polygon With the Largest Perimeter](src/2971.find-polygon-with-the-largest-perimeter.py) | Medium | O(N) | O(N) | Array, Greedy, Sorting, Prefix Sum | | |
700702
| 3005 | [Count Elements With Maximum Frequency](src/3005.count-elements-with-maximum-frequency.py) | Easy | O(N) | O(N) | Array, Hash Table, Counting | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# @lc app=leetcode id=2958 lang=python3
3+
#
4+
# [2958] Length of Longest Subarray With at Most K Frequency
5+
#
6+
7+
8+
# @lc code=start
9+
# TAGS: Array, Hash Table, Sliding Window
10+
import collections
11+
from typing import List
12+
13+
14+
class Solution:
15+
# Time and Space: O(N)
16+
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
17+
ans = index = 0
18+
counter = collections.defaultdict(int)
19+
for i, num in enumerate(nums):
20+
counter[num] += 1
21+
while counter[num] > k:
22+
counter[nums[index]] -= 1
23+
index += 1
24+
ans = max(ans, i - index + 1)
25+
return ans
26+
27+
28+
# @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# @lc app=leetcode id=2962 lang=python3
3+
#
4+
# [2962] Count Subarrays Where Max Element Appears at Least K Times
5+
#
6+
7+
8+
# @lc code=start
9+
# TAGS: Array, Sliding Window
10+
import collections
11+
from typing import List
12+
13+
14+
class Solution:
15+
# Time and Space:O(N)
16+
def countSubarrays(self, nums: List[int], k: int) -> int:
17+
ans = index = 0
18+
mx = max(nums)
19+
counter = collections.Counter()
20+
for i, num in enumerate(nums):
21+
counter[num] += 1
22+
while counter[mx] > k or nums[index] != mx:
23+
counter[nums[index]] -= 1
24+
index += 1
25+
if counter[mx] < k:
26+
continue
27+
ans += index + 1
28+
return ans
29+
30+
31+
# @lc code=end

0 commit comments

Comments
 (0)