-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcount-largest-group.py
39 lines (27 loc) · 983 Bytes
/
count-largest-group.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
from collections import Counter
class Solution:
def countLargestGroup(self, n: int) -> int:
digt_sum_counter = Counter()
largest_group = 0
for num in range(1, n + 1):
cur_sum = sum(map(int, str(num)))
digt_sum_counter[cur_sum] += 1
largest_group = max(largest_group, digt_sum_counter[cur_sum])
result = 0
for count in digt_sum_counter.values():
if count == largest_group:
result += 1
return result
class TestSolution:
def setup(self):
self.sol = Solution()
def test_case1(self):
assert self.sol.countLargestGroup(13) == 4
def test_case2(self):
assert self.sol.countLargestGroup(2) == 2
def test_case3(self):
assert self.sol.countLargestGroup(15) == 6
def test_case4(self):
assert self.sol.countLargestGroup(24) == 5
def test_case5(self):
assert self.sol.countLargestGroup(1000) == 2