跳转至

1399. 统计最大组的数目

题目描述

给定一个整数 n 。

我们需要根据数字的数位和将 1n 的数字分组。例如,数字 14 和 5 属于 同一 组,而数字 13 和 3 属于 不同 组。

返回最大组的数字数量,即元素数量 最多 的组。

 

示例 1:

输入:n = 13
输出:4
解释:总共有 9 个组,将 1 到 13 按数位求和后这些组分别是:
[1,10],[2,11],[3,12],[4,13],[5],[6],[7],[8],[9]。总共有 4 个组拥有的数字并列最多。

示例 2:

输入:n = 2
输出:2
解释:总共有 2 个大小为 1 的组 [1],[2]。

 

提示:

  • 1 <= n <= 104

解法

方法一:哈希表或数组

我们注意到数字范围不超过 \(10^4\),因此数位和的范围也不超过 \(9 \times 4 = 36\),因此我们可以用哈希表或者一个长度为 \(40\) 的数组 \(cnt\) 来统计每个数位和的个数,用一个变量 \(mx\) 表示最大的数位和个数。

我们在 \([1,..n]\) 中枚举每个数,计算其数位和 \(s\),然后将 \(cnt[s]\)\(1\),如果 \(mx \lt cnt[s]\),则更新 \(mx = cnt[s]\),并将 \(ans\) 置为 \(1\),如果 \(mx = cnt[s]\),则将 \(ans\)\(1\)

最后返回 \(ans\) 即可。

时间复杂度 \(O(n \times \log n)\),空间复杂度 \((\log n)\)。其中 \(n\) 为给定的数字。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def countLargestGroup(self, n: int) -> int:
        cnt = Counter()
        ans = mx = 0
        for i in range(1, n + 1):
            s = 0
            while i:
                s += i % 10
                i //= 10
            cnt[s] += 1
            if mx < cnt[s]:
                mx = cnt[s]
                ans = 1
            elif mx == cnt[s]:
                ans += 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public int countLargestGroup(int n) {
        int[] cnt = new int[40];
        int ans = 0, mx = 0;
        for (int i = 1; i <= n; ++i) {
            int s = 0;
            for (int x = i; x > 0; x /= 10) {
                s += x % 10;
            }
            ++cnt[s];
            if (mx < cnt[s]) {
                mx = cnt[s];
                ans = 1;
            } else if (mx == cnt[s]) {
                ++ans;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    int countLargestGroup(int n) {
        int cnt[40]{};
        int ans = 0, mx = 0;
        for (int i = 1; i <= n; ++i) {
            int s = 0;
            for (int x = i; x; x /= 10) {
                s += x % 10;
            }
            ++cnt[s];
            if (mx < cnt[s]) {
                mx = cnt[s];
                ans = 1;
            } else if (mx == cnt[s]) {
                ++ans;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func countLargestGroup(n int) (ans int) {
    cnt := [40]int{}
    mx := 0
    for i := 1; i <= n; i++ {
        s := 0
        for x := i; x > 0; x /= 10 {
            s += x % 10
        }
        cnt[s]++
        if mx < cnt[s] {
            mx = cnt[s]
            ans = 1
        } else if mx == cnt[s] {
            ans++
        }
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function countLargestGroup(n: number): number {
    const cnt: number[] = Array(40).fill(0);
    let mx = 0;
    let ans = 0;
    for (let i = 1; i <= n; ++i) {
        let s = 0;
        for (let x = i; x; x = Math.floor(x / 10)) {
            s += x % 10;
        }
        ++cnt[s];
        if (mx < cnt[s]) {
            mx = cnt[s];
            ans = 1;
        } else if (mx === cnt[s]) {
            ++ans;
        }
    }
    return ans;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
    }
}

评论