Skip to content

feat: add solution to lc problem: No.1399 #4366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions solution/1300-1399/1399.Count Largest Group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tags:

最后返回 $ans$ 即可。

时间复杂度 $O(n \times \log M)$,空间复杂度 $(\log M)$。其中 $n$ 为给定的数字,而 $M$ 是 $n$ 的数字范围
时间复杂度 $O(n \times \log n)$,空间复杂度 $(\log n)$。其中 $n$ 为给定的数字。

<!-- tabs:start -->

Expand Down Expand Up @@ -177,7 +177,7 @@ func countLargestGroup(n int) (ans int) {

```ts
function countLargestGroup(n: number): number {
const cnt: number[] = new Array(40).fill(0);
const cnt: number[] = Array(40).fill(0);
let mx = 0;
let ans = 0;
for (let i = 1; i <= n; ++i) {
Expand All @@ -197,6 +197,36 @@ function countLargestGroup(n: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_largest_group(n: i32) -> i32 {
let mut cnt = vec![0; 40];
let mut ans = 0;
let mut mx = 0;

for i in 1..=n {
let mut s = 0;
let mut x = i;
while x > 0 {
s += x % 10;
x /= 10;
}
cnt[s as usize] += 1;
if mx < cnt[s as usize] {
mx = cnt[s as usize];
ans = 1;
} else if mx == cnt[s as usize] {
ans += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
34 changes: 32 additions & 2 deletions solution/1300-1399/1399.Count Largest Group/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ We enumerate each number in $[1,..n]$, calculate its sum of digits $s$, then inc

Finally, we return $ans$.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Where $n$ is the given number, and $M$ is the range of $n$.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the given number.

<!-- tabs:start -->

Expand Down Expand Up @@ -168,7 +168,7 @@ func countLargestGroup(n int) (ans int) {

```ts
function countLargestGroup(n: number): number {
const cnt: number[] = new Array(40).fill(0);
const cnt: number[] = Array(40).fill(0);
let mx = 0;
let ans = 0;
for (let i = 1; i <= n; ++i) {
Expand All @@ -188,6 +188,36 @@ function countLargestGroup(n: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_largest_group(n: i32) -> i32 {
let mut cnt = vec![0; 40];
let mut ans = 0;
let mut mx = 0;

for i in 1..=n {
let mut s = 0;
let mut x = i;
while x > 0 {
s += x % 10;
x /= 10;
}
cnt[s as usize] += 1;
if mx < cnt[s as usize] {
mx = cnt[s as usize];
ans = 1;
} else if mx == cnt[s as usize] {
ans += 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions solution/1300-1399/1399.Count Largest Group/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
impl Solution {
pub fn count_largest_group(n: i32) -> i32 {
let mut cnt = vec![0; 40];
let mut ans = 0;
let mut mx = 0;

for i in 1..=n {
let mut s = 0;
let mut x = i;
while x > 0 {
s += x % 10;
x /= 10;
}
cnt[s as usize] += 1;
if mx < cnt[s as usize] {
mx = cnt[s as usize];
ans = 1;
} else if mx == cnt[s as usize] {
ans += 1;
}
}

ans
}
}
2 changes: 1 addition & 1 deletion solution/1300-1399/1399.Count Largest Group/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function countLargestGroup(n: number): number {
const cnt: number[] = new Array(40).fill(0);
const cnt: number[] = Array(40).fill(0);
let mx = 0;
let ans = 0;
for (let i = 1; i <= n; ++i) {
Expand Down