-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_count_distinct_slices.py
More file actions
30 lines (27 loc) · 966 Bytes
/
15_count_distinct_slices.py
File metadata and controls
30 lines (27 loc) · 966 Bytes
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
"""https://app.codility.com/programmers/lessons/15-caterpillar_method/count_distinct_slices/"""
# Time complexity:
# Iterating over every possible end-of-slice index r is O(N)
# Start-of-slice index l only increments and cannot pass r, so is O(N) work
# Overall O(2N) = O(N)
def solution(m: int, a: list) -> int:
"""
All elements of array are integers in range [0..m]
Count number of slices with no duplicate elements
"""
limit = 1_000_000_000
n = len(a)
seen = [False] * (m + 1)
res = 0
l = 0
for r in range(n):
# Discarding elements from the left (l) is safe, as any slice
# (l:k) where r < k < n will also contain a[r], thus invalid
while seen[a[r]]:
seen[a[l]] = False
l += 1
seen[a[r]] = True
# Counting the number of slices ending at r with no duplicate elements
res += r - l + 1
if res >= limit:
return limit
return res