-
Notifications
You must be signed in to change notification settings - Fork 0
/
992-subarrays-with-k-different-integers.py
54 lines (46 loc) · 1.63 KB
/
992-subarrays-with-k-different-integers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution:
# time O(N^2), space O(N)
def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
l = len(nums)
count = 0
counter = Counter()
start_idx = 0
for idx, num in enumerate(nums):
counter[num] += 1
if len(counter) < k:
continue
if len(counter) == k:
count += 1
while len(counter) > k:
counter[nums[start_idx]] -= 1
if counter[nums[start_idx]] == 0:
del counter[nums[start_idx]]
if len(counter) == k:
count += 1
start_idx += 1
temp = counter.copy()
for i in range(start_idx, idx - k + 1):
temp[nums[i]] -= 1
if temp[nums[i]] == 0:
del temp[nums[i]]
if len(temp) == k:
count += 1
elif len(temp) < k:
break
return count
# PEEKED ANSWER, time O(N), space O(N)
def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
return self.atMostK(nums, k) - self.atMostK(nums, k - 1)
def atMostK(self, nums, k):
start_idx = 0
counter = defaultdict(int)
count = 0
for end_idx, num in enumerate(nums):
counter[num] += 1
while len(counter) > k:
counter[nums[start_idx]] -= 1
if counter[nums[start_idx]] == 0:
del counter[nums[start_idx]]
start_idx += 1
count += end_idx - start_idx + 1
return count