Most Visited Sector in a Circular Track LeetCode solution in Python

1
class Solution:
2
def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
3
hash_map = {}
4
for i in range(0, len(rounds) - 1):
5
if i == 0:
6
start = rounds[i]
7
elif rounds[i] == n:
8
start = 1
9
else:
10
start = rounds[i] + 1
11
end = rounds[i + 1]
12
if start <= end:
13
for i in range(start, end + 1):
14
if i in hash_map:
15
hash_map[i] += 1
16
else:
17
hash_map[i] = 1
18
else:
19
for i in range(start, n + 1):
20
if i in hash_map:
21
hash_map[i] += 1
22
else:
23
hash_map[i] = 1
24
for i in range(1, end + 1):
25
if i in hash_map:
26
hash_map[i] += 1
27
else:
28
hash_map[i] = 1
29
k = list(hash_map.keys())
30
v = list(hash_map.values())
31
ans = []
32
m = -1
33
i = 0
34
j = 0
35
while i < len(k) and j < len(v):
36
if len(ans) == 0:
37
ans.append(k[i])
38
m = v[j]
39
elif m < v[j]:
40
ans = []
41
ans.append(k[i])
42
m = v[j]
43
elif m == v[j]:
44
ans.append(k[i])
45
i += 1
46
j += 1
47
ans = sorted(ans)
48
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0