Skip to content

Commit 1bbfa4a

Browse files
committed
update: 添加问题“1399.统计最大组的数目”的代码和题解(#896)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 8783deb commit 1bbfa4a

15 files changed

+304
-1
lines changed

Codes/1399-count-largest-group.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-23 13:17:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-23 13:19:12
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countLargestGroup(int n) {
14+
unordered_map<int, int> bin;
15+
int maxTimes = 0;
16+
for (int i = 1; i <= n; i++) {
17+
int cnt = 0, temp = i;
18+
while (temp) {
19+
cnt += temp % 10;
20+
temp /= 10;
21+
}
22+
maxTimes = max(maxTimes, ++bin[cnt]);
23+
}
24+
int ans = 0;
25+
for (auto [a, b] : bin) {
26+
ans += b == maxTimes;
27+
}
28+
return ans;
29+
}
30+
};

Codes/1399-count-largest-group.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-23 13:26:23
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-23 13:28:41
6+
*/
7+
package main
8+
9+
func countLargestGroup(n int) (ans int) {
10+
times := map[int]int{}
11+
maxTimes := 0
12+
for i := 1; i <= n; i++ {
13+
cnt := 0
14+
for temp := i; temp > 0; temp /= 10 {
15+
cnt += temp % 10
16+
}
17+
times[cnt]++
18+
maxTimes = max(maxTimes, times[cnt])
19+
}
20+
for _, val := range times {
21+
if val == maxTimes {
22+
ans++
23+
}
24+
}
25+
return
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-23 13:23:09
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-23 13:24:57
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
10+
class Solution {
11+
public int countLargestGroup(int n) {
12+
Map<Integer, Integer> times = new HashMap<>();
13+
int maxTimes = 0;
14+
for (int i = 1; i <= n; i++) {
15+
int cnt = 0, temp = i;
16+
while (temp > 0) {
17+
cnt += temp % 10;
18+
temp /= 10;
19+
}
20+
maxTimes = Math.max(maxTimes, times.merge(cnt, 1, Integer::sum));
21+
}
22+
int ans = 0;
23+
for (Map.Entry<Integer, Integer> pair : times.entrySet()) {
24+
if (pair.getValue() == maxTimes) {
25+
ans++;
26+
}
27+
}
28+
return ans;
29+
}
30+
}

Codes/1399-count-largest-group.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-23 13:20:37
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-23 13:22:37
6+
'''
7+
from collections import defaultdict
8+
9+
class Solution:
10+
def countLargestGroup(self, n: int) -> int:
11+
counter = defaultdict(int)
12+
maxTimes = 0
13+
for i in range(1, n + 1):
14+
cnt = sum(map(int, str(i)))
15+
counter[cnt] += 1
16+
maxTimes = max(maxTimes, counter[cnt])
17+
return sum(b == maxTimes for a, b in counter.items())

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@
519519
|1375.二进制字符串前缀一致的次数|中等|<a href="https://leetcode.cn/problems/number-of-times-binary-string-is-prefix-aligned/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/14/LeetCode%201375.%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E7%BC%80%E4%B8%80%E8%87%B4%E7%9A%84%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131213418" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-times-binary-string-is-prefix-aligned/solutions/2309139/letmefly-1375er-jin-zhi-zi-fu-chuan-qian-odu7/" target="_blank">LeetCode题解</a>|
520520
|1376.通知所有员工所需的时间|中等|<a href="https://leetcode.cn/problems/time-needed-to-inform-all-employees/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/01/LeetCode%201376.%E9%80%9A%E7%9F%A5%E6%89%80%E6%9C%89%E5%91%98%E5%B7%A5%E6%89%80%E9%9C%80%E7%9A%84%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130458959" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/time-needed-to-inform-all-employees/solutions/2252783/letmefly-1376tong-zhi-suo-you-yuan-gong-v721e/" target="_blank">LeetCode题解</a>|
521521
|1379.找出克隆二叉树中的相同节点|简单|<a href="https://leetcode.cn/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/03/LeetCode%201379.%E6%89%BE%E5%87%BA%E5%85%8B%E9%9A%86%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E7%9A%84%E7%9B%B8%E5%90%8C%E8%8A%82%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137341930" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/solutions/2722126/letmefly-1379zhao-chu-ke-long-er-cha-shu-717e/" target="_blank">LeetCode题解</a>|
522+
|1399.统计最大组的数目|简单|<a href="https://leetcode.cn/problems/count-largest-group/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/23/LeetCode%201399.%E7%BB%9F%E8%AE%A1%E6%9C%80%E5%A4%A7%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147448113" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-largest-group/solutions/3659689/letmefly-1399tong-ji-zui-da-zu-de-shu-mu-09kx/" target="_blank">LeetCode题解</a>|
522523
|1402.做菜顺序|困难|<a href="https://leetcode.cn/problems/reducing-dishes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/22/LeetCode%201402.%E5%81%9A%E8%8F%9C%E9%A1%BA%E5%BA%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133974648" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reducing-dishes/solutions/2493595/letmefly-1402zuo-cai-shun-xu-pai-xu-er-f-s5te/" target="_blank">LeetCode题解</a>|
523524
|1403.非递增顺序的最小子序列|简单|<a href="https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/04/LeetCode%201403.%E9%9D%9E%E9%80%92%E5%A2%9E%E9%A1%BA%E5%BA%8F%E7%9A%84%E6%9C%80%E5%B0%8F%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126155397" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-subsequence-in-non-increasing-order/solution/letmefly-1403fei-di-zeng-shun-xu-de-zui-h56ht/" target="_blank">LeetCode题解</a>|
524525
|1408.数组中的字符串匹配|简单|<a href="https://leetcode.cn/problems/string-matching-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/06/LeetCode%201408.%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%B9%E9%85%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126189255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/string-matching-in-an-array/solution/letmefly-1408shu-zu-zhong-de-zi-fu-chuan-v90u/" target="_blank">LeetCode题解</a>|
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
---
2+
title: 1399.统计最大组的数目:哈希表模拟(简单题简单做)
3+
date: 2025-04-23 13:29:46
4+
tags: [题解, LeetCode, 简单, 哈希表, 数学]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1399.统计最大组的数目:哈希表模拟(简单题简单做)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-largest-group/](https://leetcode.cn/problems/count-largest-group/)
11+
12+
<p>给你一个整数 <code>n</code>&nbsp;。请你先求出从 <code>1</code>&nbsp;到 <code>n</code> 的每个整数 10 进制表示下的数位和(每一位上的数字相加),然后把数位和相等的数字放到同一个组中。</p>
13+
14+
<p>请你统计每个组中的数字数目,并返回数字数目并列最多的组有多少个。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>示例 1:</strong></p>
19+
20+
<pre><strong>输入:</strong>n = 13
21+
<strong>输出:</strong>4
22+
<strong>解释:</strong>总共有 9 个组,将 1 到 13 按数位求和后这些组分别是:
23+
[1,10],[2,11],[3,12],[4,13],[5],[6],[7],[8],[9]。总共有 4 个组拥有的数字并列最多。
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<pre><strong>输入:</strong>n = 2
29+
<strong>输出:</strong>2
30+
<strong>解释:</strong>总共有 2 个大小为 1 的组 [1],[2]。
31+
</pre>
32+
33+
<p><strong>示例 3:</strong></p>
34+
35+
<pre><strong>输入:</strong>n = 15
36+
<strong>输出:</strong>6
37+
</pre>
38+
39+
<p><strong>示例 4:</strong></p>
40+
41+
<pre><strong>输入:</strong>n = 24
42+
<strong>输出:</strong>5
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n &lt;= 10^4</code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法:哈希表
56+
57+
如何统计一个数字十进制下的每一位之和?
58+
59+
> 在这个数字(假设为$t$)不为0时,不断累加这个数的个位($t\%10$),并将这个数除以$10$(向下取整)。
60+
61+
使用一个哈希表,键为“一个数的每一位之和”,值为这个“和”的出现次数。
62+
63+
统计最大的“值”,然后统计有多少个键值对的值是这个最大的值。
64+
65+
+ 时间复杂度$O(n\log_{10}n)$
66+
+ 空间复杂度$O(n)$
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
/*
74+
* @Author: LetMeFly
75+
* @Date: 2025-04-23 13:17:46
76+
* @LastEditors: LetMeFly.xyz
77+
* @LastEditTime: 2025-04-23 13:19:12
78+
*/
79+
80+
class Solution {
81+
public:
82+
int countLargestGroup(int n) {
83+
unordered_map<int, int> bin;
84+
int maxTimes = 0;
85+
for (int i = 1; i <= n; i++) {
86+
int cnt = 0, temp = i;
87+
while (temp) {
88+
cnt += temp % 10;
89+
temp /= 10;
90+
}
91+
maxTimes = max(maxTimes, ++bin[cnt]);
92+
}
93+
int ans = 0;
94+
for (auto [a, b] : bin) {
95+
ans += b == maxTimes;
96+
}
97+
return ans;
98+
}
99+
};
100+
```
101+
102+
#### Python
103+
104+
```python
105+
'''
106+
Author: LetMeFly
107+
Date: 2025-04-23 13:20:37
108+
LastEditors: LetMeFly.xyz
109+
LastEditTime: 2025-04-23 13:22:37
110+
'''
111+
from collections import defaultdict
112+
113+
class Solution:
114+
def countLargestGroup(self, n: int) -> int:
115+
counter = defaultdict(int)
116+
maxTimes = 0
117+
for i in range(1, n + 1):
118+
cnt = sum(map(int, str(i)))
119+
counter[cnt] += 1
120+
maxTimes = max(maxTimes, counter[cnt])
121+
return sum(b == maxTimes for a, b in counter.items())
122+
```
123+
124+
#### Java
125+
126+
```java
127+
/*
128+
* @Author: LetMeFly
129+
* @Date: 2025-04-23 13:23:09
130+
* @LastEditors: LetMeFly.xyz
131+
* @LastEditTime: 2025-04-23 13:24:57
132+
*/
133+
import java.util.Map;
134+
import java.util.HashMap;
135+
136+
class Solution {
137+
public int countLargestGroup(int n) {
138+
Map<Integer, Integer> times = new HashMap<>();
139+
int maxTimes = 0;
140+
for (int i = 1; i <= n; i++) {
141+
int cnt = 0, temp = i;
142+
while (temp > 0) {
143+
cnt += temp % 10;
144+
temp /= 10;
145+
}
146+
maxTimes = Math.max(maxTimes, times.merge(cnt, 1, Integer::sum));
147+
}
148+
int ans = 0;
149+
for (Map.Entry<Integer, Integer> pair : times.entrySet()) {
150+
if (pair.getValue() == maxTimes) {
151+
ans++;
152+
}
153+
}
154+
return ans;
155+
}
156+
}
157+
```
158+
159+
#### Go
160+
161+
```go
162+
/*
163+
* @Author: LetMeFly
164+
* @Date: 2025-04-23 13:26:23
165+
* @LastEditors: LetMeFly.xyz
166+
* @LastEditTime: 2025-04-23 13:28:41
167+
*/
168+
package main
169+
170+
func countLargestGroup(n int) (ans int) {
171+
times := map[int]int{}
172+
maxTimes := 0
173+
for i := 1; i <= n; i++ {
174+
cnt := 0
175+
for temp := i; temp > 0; temp /= 10 {
176+
cnt += temp % 10
177+
}
178+
times[cnt]++
179+
maxTimes = max(maxTimes, times[cnt])
180+
}
181+
for _, val := range times {
182+
if val == maxTimes {
183+
ans++
184+
}
185+
}
186+
return
187+
}
188+
```
189+
190+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147448113)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/23/LeetCode%201399.%E7%BB%9F%E8%AE%A1%E6%9C%80%E5%A4%A7%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/)哦~
191+
>
192+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,10 @@ categories: [自用]
11051105
|ferrous|adj. 含铁的,铁的|
11061106
|||
11071107
|stammer|v. 口吃,结结巴巴地说<br>n. 口吃,结巴|
1108+
|||
1109+
|terrace|n. 露天平台,阳台,阶梯看台,梯田|
1110+
|||
1111+
|bottle-neck|n. 瓶颈,道路上的狭窄处|
11081112

11091113
<p class="wordCounts">单词收录总数</p>
11101114

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ xx型
252252
|お酒(おさけ)||
253253
|てりやき|照烧|
254254
|てんぷら|天妇罗|
255+
|味噌汁(みそしる)|味增汤|
255256
|なっとう|纳豆|
256257
|ごはん|米饭|
257258
|みず||
@@ -268,6 +269,7 @@ xx型
268269
|お菓子(おかし)|零食|
269270
|卵(たまご)|鸡蛋|
270271
|シリアル|麦片|
272+
|蕎麦(そば)|荞麦(面)|
271273
|おにぎり|饭团🍙|
272274
|塩(しお)||
273275
|醬油(しょうゆ)|酱油|
@@ -890,6 +892,7 @@ xx型
890892
|でき|会/能做到|
891893
|違う(ちがう)|不对/不同|
892894
|素敵(すてき)|好/漂亮|
895+
|必ず(かならず)|一定|
893896
|||
894897
|牛肉(ぎゅうにく)|牛肉|
895898
|豚肉(ぶたにく)|猪肉|
@@ -1312,7 +1315,7 @@ xx型
13121315
|部屋は六階でした。<br/>房间在六楼。|
13131316
|スタッフに言いました。<br/>跟员工说了。|
13141317
|部屋のシャワーはぬるかったです。<br/>房间的水不够热(只是温的)。|
1315-
|<br/>|
1318+
|必ず悪いレビューをたくさん書きます。<br/>我一定会写很多差评!|
13161319
|<br/>|
13171320
|<br/>|
13181321
|<br/>|
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)