Skip to content

Commit 3774d75

Browse files
authored
Create distinct-numbers-in-each-subarray.py
1 parent 1fa765b commit 3774d75

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n)
2+
# Space: O(k)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def distinctNumbers(self, nums, k):
9+
"""
10+
:type nums: List[int]
11+
:type k: int
12+
:rtype: List[int]
13+
"""
14+
result = []
15+
count = collections.Counter()
16+
for i, num in enumerate(nums):
17+
count[num] += 1
18+
if i >= k:
19+
count[nums[i-k]] -= 1
20+
if not count[nums[i-k]]:
21+
del count[nums[i-k]]
22+
if i+1 >= k:
23+
result.append(len(count))
24+
return result
25+

0 commit comments

Comments
 (0)