Skip to content

Commit 985409d

Browse files
committed
feat: add solutions to lc problem: No.1399
No.1399.Count Largest Group
1 parent dc6f58e commit 985409d

File tree

7 files changed

+190
-121
lines changed

7 files changed

+190
-121
lines changed

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

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@
5151

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

54-
**方法一:哈希表模拟**
54+
**方法一:哈希表或数组**
55+
56+
我们注意到数字范围不超过 $10^4$,因此数位和的范围也不超过 $9 \times 4 = 36$,因此我们可以用哈希表或者一个长度为 $40$ 的数组 $cnt$ 来统计每个数位和的个数,用一个变量 $mx$ 表示最大的数位和个数。
57+
58+
我们在 $[1,..n]$ 中枚举每个数,计算其数位和 $s$,然后将 $cnt[s]$ 加 $1$,如果 $mx \lt cnt[s]$,则更新 $mx = cnt[s]$,并将 $ans$ 置为 $1$,如果 $mx = cnt[s]$,则将 $ans$ 加 $1$。
59+
60+
最后返回 $ans$ 即可。
61+
62+
时间复杂度 $O(n \times \log M)$,空间复杂度 $(\log M)$。其中 $n$ 为给定的数字,而 $M$ 是 $n$ 的数字范围。
5563

5664
<!-- tabs:start -->
5765

@@ -63,14 +71,17 @@
6371
class Solution:
6472
def countLargestGroup(self, n: int) -> int:
6573
cnt = Counter()
66-
ans, mx = 0, 0
74+
ans = mx = 0
6775
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]
76+
s = 0
77+
while i:
78+
s += i % 10
79+
i //= 10
80+
cnt[s] += 1
81+
if mx < cnt[s]:
82+
mx = cnt[s]
7283
ans = 1
73-
elif mx == cnt[t]:
84+
elif mx == cnt[s]:
7485
ans += 1
7586
return ans
7687
```
@@ -83,19 +94,17 @@ class Solution:
8394
class Solution {
8495
public int countLargestGroup(int n) {
8596
int[] cnt = new int[40];
86-
int mx = 0, ans = 0;
97+
int ans = 0, mx = 0;
8798
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;
99+
int s = 0;
100+
for (int x = i; x > 0; x /= 10) {
101+
s += x % 10;
93102
}
94-
++cnt[t];
95-
if (mx < cnt[t]) {
96-
mx = cnt[t];
103+
++cnt[s];
104+
if (mx < cnt[s]) {
105+
mx = cnt[s];
97106
ans = 1;
98-
} else if (mx == cnt[t]) {
107+
} else if (mx == cnt[s]) {
99108
++ans;
100109
}
101110
}
@@ -110,21 +119,20 @@ class Solution {
110119
class Solution {
111120
public:
112121
int countLargestGroup(int n) {
113-
vector<int> cnt(40);
114-
int mx = 0, ans = 0;
122+
int cnt[40]{};
123+
int ans = 0, mx = 0;
115124
for (int i = 1; i <= n; ++i) {
116-
int t = 0;
117-
int j = i;
118-
while (j) {
119-
t += j % 10;
120-
j /= 10;
125+
int s = 0;
126+
for (int x = i; x; x /= 10) {
127+
s += x % 10;
121128
}
122-
++cnt[t];
123-
if (mx < cnt[t]) {
124-
mx = cnt[t];
129+
++cnt[s];
130+
if (mx < cnt[s]) {
131+
mx = cnt[s];
125132
ans = 1;
126-
} else if (mx == cnt[t])
133+
} else if (mx == cnt[s]) {
127134
++ans;
135+
}
128136
}
129137
return ans;
130138
}
@@ -134,25 +142,47 @@ public:
134142
### **Go**
135143
136144
```go
137-
func countLargestGroup(n int) int {
138-
cnt := make([]int, 40)
139-
mx, ans := 0, 0
145+
func countLargestGroup(n int) (ans int) {
146+
cnt := [40]int{}
147+
mx := 0
140148
for i := 1; i <= n; i++ {
141-
t := 0
142-
j := i
143-
for j != 0 {
144-
t += j % 10
145-
j /= 10
149+
s := 0
150+
for x := i; x > 0; x /= 10 {
151+
s += x % 10
146152
}
147-
cnt[t]++
148-
if mx < cnt[t] {
149-
mx = cnt[t]
153+
cnt[s]++
154+
if mx < cnt[s] {
155+
mx = cnt[s]
150156
ans = 1
151-
} else if mx == cnt[t] {
157+
} else if mx == cnt[s] {
152158
ans++
153159
}
154160
}
155-
return ans
161+
return
162+
}
163+
```
164+
165+
### **TypeScript**
166+
167+
```ts
168+
function countLargestGroup(n: number): number {
169+
const cnt: number[] = new Array(40).fill(0);
170+
let mx = 0;
171+
let ans = 0;
172+
for (let i = 1; i <= n; ++i) {
173+
let s = 0;
174+
for (let x = i; x; x = Math.floor(x / 10)) {
175+
s += x % 10;
176+
}
177+
++cnt[s];
178+
if (mx < cnt[s]) {
179+
mx = cnt[s];
180+
ans = 1;
181+
} else if (mx === cnt[s]) {
182+
++ans;
183+
}
184+
}
185+
return ans;
156186
}
157187
```
158188

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

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ There are 4 groups with largest size.
4646
class Solution:
4747
def countLargestGroup(self, n: int) -> int:
4848
cnt = Counter()
49-
ans, mx = 0, 0
49+
ans = mx = 0
5050
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]
51+
s = 0
52+
while i:
53+
s += i % 10
54+
i //= 10
55+
cnt[s] += 1
56+
if mx < cnt[s]:
57+
mx = cnt[s]
5558
ans = 1
56-
elif mx == cnt[t]:
59+
elif mx == cnt[s]:
5760
ans += 1
5861
return ans
5962
```
@@ -64,19 +67,17 @@ class Solution:
6467
class Solution {
6568
public int countLargestGroup(int n) {
6669
int[] cnt = new int[40];
67-
int mx = 0, ans = 0;
70+
int ans = 0, mx = 0;
6871
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;
72+
int s = 0;
73+
for (int x = i; x > 0; x /= 10) {
74+
s += x % 10;
7475
}
75-
++cnt[t];
76-
if (mx < cnt[t]) {
77-
mx = cnt[t];
76+
++cnt[s];
77+
if (mx < cnt[s]) {
78+
mx = cnt[s];
7879
ans = 1;
79-
} else if (mx == cnt[t]) {
80+
} else if (mx == cnt[s]) {
8081
++ans;
8182
}
8283
}
@@ -91,21 +92,20 @@ class Solution {
9192
class Solution {
9293
public:
9394
int countLargestGroup(int n) {
94-
vector<int> cnt(40);
95-
int mx = 0, ans = 0;
95+
int cnt[40]{};
96+
int ans = 0, mx = 0;
9697
for (int i = 1; i <= n; ++i) {
97-
int t = 0;
98-
int j = i;
99-
while (j) {
100-
t += j % 10;
101-
j /= 10;
98+
int s = 0;
99+
for (int x = i; x; x /= 10) {
100+
s += x % 10;
102101
}
103-
++cnt[t];
104-
if (mx < cnt[t]) {
105-
mx = cnt[t];
102+
++cnt[s];
103+
if (mx < cnt[s]) {
104+
mx = cnt[s];
106105
ans = 1;
107-
} else if (mx == cnt[t])
106+
} else if (mx == cnt[s]) {
108107
++ans;
108+
}
109109
}
110110
return ans;
111111
}
@@ -115,25 +115,47 @@ public:
115115
### **Go**
116116
117117
```go
118-
func countLargestGroup(n int) int {
119-
cnt := make([]int, 40)
120-
mx, ans := 0, 0
118+
func countLargestGroup(n int) (ans int) {
119+
cnt := [40]int{}
120+
mx := 0
121121
for i := 1; i <= n; i++ {
122-
t := 0
123-
j := i
124-
for j != 0 {
125-
t += j % 10
126-
j /= 10
122+
s := 0
123+
for x := i; x > 0; x /= 10 {
124+
s += x % 10
127125
}
128-
cnt[t]++
129-
if mx < cnt[t] {
130-
mx = cnt[t]
126+
cnt[s]++
127+
if mx < cnt[s] {
128+
mx = cnt[s]
131129
ans = 1
132-
} else if mx == cnt[t] {
130+
} else if mx == cnt[s] {
133131
ans++
134132
}
135133
}
136-
return ans
134+
return
135+
}
136+
```
137+
138+
### **TypeScript**
139+
140+
```ts
141+
function countLargestGroup(n: number): number {
142+
const cnt: number[] = new Array(40).fill(0);
143+
let mx = 0;
144+
let ans = 0;
145+
for (let i = 1; i <= n; ++i) {
146+
let s = 0;
147+
for (let x = i; x; x = Math.floor(x / 10)) {
148+
s += x % 10;
149+
}
150+
++cnt[s];
151+
if (mx < cnt[s]) {
152+
mx = cnt[s];
153+
ans = 1;
154+
} else if (mx === cnt[s]) {
155+
++ans;
156+
}
157+
}
158+
return ans;
137159
}
138160
```
139161

solution/1300-1399/1399.Count Largest Group/Solution.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
22
public:
33
int countLargestGroup(int n) {
4-
vector<int> cnt(40);
5-
int mx = 0, ans = 0;
4+
int cnt[40]{};
5+
int ans = 0, mx = 0;
66
for (int i = 1; i <= n; ++i) {
7-
int t = 0;
8-
int j = i;
9-
while (j) {
10-
t += j % 10;
11-
j /= 10;
7+
int s = 0;
8+
for (int x = i; x; x /= 10) {
9+
s += x % 10;
1210
}
13-
++cnt[t];
14-
if (mx < cnt[t]) {
15-
mx = cnt[t];
11+
++cnt[s];
12+
if (mx < cnt[s]) {
13+
mx = cnt[s];
1614
ans = 1;
17-
} else if (mx == cnt[t])
15+
} else if (mx == cnt[s]) {
1816
++ans;
17+
}
1918
}
2019
return ans;
2120
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
func countLargestGroup(n int) int {
2-
cnt := make([]int, 40)
3-
mx, ans := 0, 0
1+
func countLargestGroup(n int) (ans int) {
2+
cnt := [40]int{}
3+
mx := 0
44
for i := 1; i <= n; i++ {
5-
t := 0
6-
j := i
7-
for j != 0 {
8-
t += j % 10
9-
j /= 10
5+
s := 0
6+
for x := i; x > 0; x /= 10 {
7+
s += x % 10
108
}
11-
cnt[t]++
12-
if mx < cnt[t] {
13-
mx = cnt[t]
9+
cnt[s]++
10+
if mx < cnt[s] {
11+
mx = cnt[s]
1412
ans = 1
15-
} else if mx == cnt[t] {
13+
} else if mx == cnt[s] {
1614
ans++
1715
}
1816
}
19-
return ans
17+
return
2018
}

solution/1300-1399/1399.Count Largest Group/Solution.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution {
22
public int countLargestGroup(int n) {
33
int[] cnt = new int[40];
4-
int mx = 0, ans = 0;
4+
int ans = 0, mx = 0;
55
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;
6+
int s = 0;
7+
for (int x = i; x > 0; x /= 10) {
8+
s += x % 10;
119
}
12-
++cnt[t];
13-
if (mx < cnt[t]) {
14-
mx = cnt[t];
10+
++cnt[s];
11+
if (mx < cnt[s]) {
12+
mx = cnt[s];
1513
ans = 1;
16-
} else if (mx == cnt[t]) {
14+
} else if (mx == cnt[s]) {
1715
++ans;
1816
}
1917
}

0 commit comments

Comments
 (0)