Skip to content

Commit de9961f

Browse files
committed
feat: add solutions to lc problem: No.1399
No.1399.Count Largest Group
1 parent 8bfff44 commit de9961f

File tree

6 files changed

+260
-2
lines changed

6 files changed

+260
-2
lines changed

solution/1300-1399/1399.Count Largest Group/README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,112 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:哈希表模拟**
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
61-
63+
class Solution:
64+
def countLargestGroup(self, n: int) -> int:
65+
cnt = Counter()
66+
ans, mx = 0, 0
67+
for i in range(1, n + 1):
68+
t = sum(int(v) for v in str(i))
69+
cnt[t] += 1
70+
if mx < cnt[t]:
71+
mx = cnt[t]
72+
ans = 1
73+
elif mx == cnt[t]:
74+
ans += 1
75+
return ans
6276
```
6377

6478
### **Java**
6579

6680
<!-- 这里可写当前语言的特殊实现逻辑 -->
6781

6882
```java
83+
class Solution {
84+
public int countLargestGroup(int n) {
85+
int[] cnt = new int[40];
86+
int mx = 0, ans = 0;
87+
for (int i = 1; i <= n; ++i) {
88+
int t = 0;
89+
int j = i;
90+
while (j != 0) {
91+
t += j % 10;
92+
j /= 10;
93+
}
94+
++cnt[t];
95+
if (mx < cnt[t]) {
96+
mx = cnt[t];
97+
ans = 1;
98+
} else if (mx == cnt[t]) {
99+
++ans;
100+
}
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int countLargestGroup(int n) {
113+
vector<int> cnt(40);
114+
int mx = 0, ans = 0;
115+
for (int i = 1; i <= n; ++i)
116+
{
117+
int t = 0;
118+
int j = i;
119+
while (j)
120+
{
121+
t += j % 10;
122+
j /= 10;
123+
}
124+
++cnt[t];
125+
if (mx < cnt[t])
126+
{
127+
mx = cnt[t];
128+
ans = 1;
129+
}
130+
else if (mx == cnt[t]) ++ans;
131+
}
132+
return ans;
133+
}
134+
};
135+
```
69136
137+
### **Go**
138+
139+
```go
140+
func countLargestGroup(n int) int {
141+
cnt := make([]int, 40)
142+
mx, ans := 0, 0
143+
for i := 1; i <= n; i++ {
144+
t := 0
145+
j := i
146+
for j != 0 {
147+
t += j % 10
148+
j /= 10
149+
}
150+
cnt[t]++
151+
if mx < cnt[t] {
152+
mx = cnt[t]
153+
ans = 1
154+
} else if mx == cnt[t] {
155+
ans++
156+
}
157+
}
158+
return ans
159+
}
70160
```
71161

72162
### **...**

solution/1300-1399/1399.Count Largest Group/README_EN.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,101 @@ There are 4 groups with largest size.
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def countLargestGroup(self, n: int) -> int:
48+
cnt = Counter()
49+
ans, mx = 0, 0
50+
for i in range(1, n + 1):
51+
t = sum(int(v) for v in str(i))
52+
cnt[t] += 1
53+
if mx < cnt[t]:
54+
mx = cnt[t]
55+
ans = 1
56+
elif mx == cnt[t]:
57+
ans += 1
58+
return ans
4759
```
4860

4961
### **Java**
5062

5163
```java
64+
class Solution {
65+
public int countLargestGroup(int n) {
66+
int[] cnt = new int[40];
67+
int mx = 0, ans = 0;
68+
for (int i = 1; i <= n; ++i) {
69+
int t = 0;
70+
int j = i;
71+
while (j != 0) {
72+
t += j % 10;
73+
j /= 10;
74+
}
75+
++cnt[t];
76+
if (mx < cnt[t]) {
77+
mx = cnt[t];
78+
ans = 1;
79+
} else if (mx == cnt[t]) {
80+
++ans;
81+
}
82+
}
83+
return ans;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int countLargestGroup(int n) {
94+
vector<int> cnt(40);
95+
int mx = 0, ans = 0;
96+
for (int i = 1; i <= n; ++i)
97+
{
98+
int t = 0;
99+
int j = i;
100+
while (j)
101+
{
102+
t += j % 10;
103+
j /= 10;
104+
}
105+
++cnt[t];
106+
if (mx < cnt[t])
107+
{
108+
mx = cnt[t];
109+
ans = 1;
110+
}
111+
else if (mx == cnt[t]) ++ans;
112+
}
113+
return ans;
114+
}
115+
};
116+
```
52117
118+
### **Go**
119+
120+
```go
121+
func countLargestGroup(n int) int {
122+
cnt := make([]int, 40)
123+
mx, ans := 0, 0
124+
for i := 1; i <= n; i++ {
125+
t := 0
126+
j := i
127+
for j != 0 {
128+
t += j % 10
129+
j /= 10
130+
}
131+
cnt[t]++
132+
if mx < cnt[t] {
133+
mx = cnt[t]
134+
ans = 1
135+
} else if mx == cnt[t] {
136+
ans++
137+
}
138+
}
139+
return ans
140+
}
53141
```
54142

55143
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int countLargestGroup(int n) {
4+
vector<int> cnt(40);
5+
int mx = 0, ans = 0;
6+
for (int i = 1; i <= n; ++i)
7+
{
8+
int t = 0;
9+
int j = i;
10+
while (j)
11+
{
12+
t += j % 10;
13+
j /= 10;
14+
}
15+
++cnt[t];
16+
if (mx < cnt[t])
17+
{
18+
mx = cnt[t];
19+
ans = 1;
20+
}
21+
else if (mx == cnt[t]) ++ans;
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func countLargestGroup(n int) int {
2+
cnt := make([]int, 40)
3+
mx, ans := 0, 0
4+
for i := 1; i <= n; i++ {
5+
t := 0
6+
j := i
7+
for j != 0 {
8+
t += j % 10
9+
j /= 10
10+
}
11+
cnt[t]++
12+
if mx < cnt[t] {
13+
mx = cnt[t]
14+
ans = 1
15+
} else if mx == cnt[t] {
16+
ans++
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int countLargestGroup(int n) {
3+
int[] cnt = new int[40];
4+
int mx = 0, ans = 0;
5+
for (int i = 1; i <= n; ++i) {
6+
int t = 0;
7+
int j = i;
8+
while (j != 0) {
9+
t += j % 10;
10+
j /= 10;
11+
}
12+
++cnt[t];
13+
if (mx < cnt[t]) {
14+
mx = cnt[t];
15+
ans = 1;
16+
} else if (mx == cnt[t]) {
17+
++ans;
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def countLargestGroup(self, n: int) -> int:
3+
cnt = Counter()
4+
ans, mx = 0, 0
5+
for i in range(1, n + 1):
6+
t = sum(int(v) for v in str(i))
7+
cnt[t] += 1
8+
if mx < cnt[t]:
9+
mx = cnt[t]
10+
ans = 1
11+
elif mx == cnt[t]:
12+
ans += 1
13+
return ans

0 commit comments

Comments
 (0)