aboutsummaryrefslogtreecommitdiff
path: root/len_longest_subarr_k.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /len_longest_subarr_k.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'len_longest_subarr_k.py')
-rw-r--r--len_longest_subarr_k.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/len_longest_subarr_k.py b/len_longest_subarr_k.py
new file mode 100644
index 0000000..b97a849
--- /dev/null
+++ b/len_longest_subarr_k.py
@@ -0,0 +1,35 @@
+# 2958. Length of Longest Subarray With at Most K Frequency
+from collections import Counter
+
+"""
+given an integer array 'nums' and an integer 'k'. the frequency of an element
+x is the number of times it occurs in an array. an array is called good if
+the frequency of each element in this array is less than or equal to 'k'.
+return the length of the longest good subarray of 'nums' a subarray is a
+contiguous non empty sequence of elements within an array
+"""
+
+
+class Solution(object):
+ def maxSubarrayLength(self, nums, k):
+ """
+ :type nums: List[int]
+ :type k: int
+ :rtype: int
+ """
+ cnt = Counter()
+ ans, i = 0, 0
+ for j in range(len(nums)):
+ cnt[nums[j]] += 1
+ while cnt[nums[j]] > k:
+ cnt[nums[i]] -= 1
+ i += 1
+ ans = max(ans, j - i + 1)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxSubarrayLength(nums=[1, 2, 3, 1, 2, 3, 1, 2], k=2))
+ print(obj.maxSubarrayLength(nums=[1, 2, 1, 2, 1, 2, 1, 2], k=1))
+ print(obj.maxSubarrayLength(nums=[5, 5, 5, 5, 5, 5, 5], k=4))