Skip to content

Commit e3b8751

Browse files
committed
Sync LeetCode submission Runtime - 71 ms (14.25%), Memory - 17.9 MB (53.00%)
1 parent 7ae4aa0 commit e3b8751

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

1500-count-largest-group/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>You are given an integer <code>n</code>.</p>
2+
3+
<p>Each number from <code>1</code> to <code>n</code> is grouped according to the sum of its digits.</p>
4+
5+
<p>Return <em>the number of groups that have the largest size</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> n = 13
12+
<strong>Output:</strong> 4
13+
<strong>Explanation:</strong> There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:
14+
[1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9].
15+
There are 4 groups with largest size.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> n = 2
22+
<strong>Output:</strong> 2
23+
<strong>Explanation:</strong> There are 2 groups [1], [2] of size 1.
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
31+
</ul>

1500-count-largest-group/solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach: Hashmap
2+
3+
# Time: O(n log n)
4+
# Space: O(log n)
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def countLargestGroup(self, n: int) -> int:
10+
hashmap = Counter()
11+
12+
for i in range(1, n + 1):
13+
key = sum([int(x) for x in str(i)])
14+
hashmap[key] += 1
15+
16+
max_value = max(hashmap.values())
17+
count = sum(1 for v in hashmap.values() if v == max_value)
18+
return count
19+

0 commit comments

Comments
 (0)