class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
n, res = len(nums), 0
for i in range(n):
count = defaultdict(int)
for j in range(i, n):
count[nums[j]] += 1
if count[nums[j]] > k:
break
res = max(res, j - i + 1)
return resclass Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
res = 0
count = defaultdict(int)
l = 0
for r in range(len(nums)):
count[nums[r]] += 1
while count[nums[r]] > k:
count[nums[l]] -= 1
l += 1
res = max(res, r - l + 1)
return resclass Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
count = defaultdict(int)
l = res = 0
cnt = 0 # count of numbers with freq > k
for r in range(len(nums)):
count[nums[r]] += 1
cnt += count[nums[r]] > k
if cnt > 0:
cnt -= count[nums[l]] > k
count[nums[l]] -= 1
l += 1
return len(nums) - l