Count Largest Group LeetCode solution in Python

1
def compute(num):
2
if num < 10:
3
return num
4

5
newVal = 0
6

7
while num > 0:
8
last = num % 10
9
newVal += last
10
num /= 10
11

12
return newVal
13

14

15
class Solution(object):
16
def countLargestGroup(self, n):
17
"""
18
:type n: int
19
:rtype: int
20
"""
21
myMap = {}
22

23
for i in range(1, n + 1):
24
val = compute(i)
25

26
if val in myMap.keys():
27
myMap.get(val).append(i)
28
else:
29
myMap[val] = [i]
30

31
maxLen = 0
32

33
for n in myMap.values():
34
maxLen = max(maxLen, len(n))
35

36
ans = 0
37

38
for n in myMap.values():
39
if len(n) == maxLen:
40
ans += 1
41

42
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0