diff options
author | Jack Sangdahl <[email protected]> | 2025-04-23 00:22:32 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-04-23 00:22:32 -0600 |
commit | 5ac705b1894f3c85f6736c62cd5c0e44f254736e (patch) | |
tree | 8e8d7b906864b1e7ba7ebb522ea8491b409a7389 | |
parent | e452b865cbb2f6af0c97380721f009f9672ad432 (diff) | |
download | leetcode-5ac705b1894f3c85f6736c62cd5c0e44f254736e.tar.gz leetcode-5ac705b1894f3c85f6736c62cd5c0e44f254736e.zip |
1399. count largest group
-rw-r--r-- | count_largest_group.c | 37 | ||||
-rw-r--r-- | count_largest_group.py | 39 |
2 files changed, 76 insertions, 0 deletions
diff --git a/count_largest_group.c b/count_largest_group.c new file mode 100644 index 0000000..4d9063b --- /dev/null +++ b/count_largest_group.c @@ -0,0 +1,37 @@ +// 1399. Count Largest Group +#include "leetcode.h" + +/* + * you are given an integer 'n'. each number from 1 to 'n' is grouped according + * to the sum of its digits. return the number of groups that have the largest + * size. + */ + +int get_num(int n) { + int res = 0; + while (n) { + res += n % 10; + n /= 10; + } + return res; +} + +int countLargestGroup(int n) { + int arr[37] = {0}, max = 0; + for (int i = 1; i <= n; i++) { + int curr = get_num(i); + arr[curr]++; + if (arr[curr] > max) + max = arr[curr]; + } + int cnt = 0; + for (int i = 1; i <= 36; i++) + if (arr[i] == max) + cnt++; + return cnt; +} + +int main() { + printf("%d\n", countLargestGroup(13)); // expect: 4 + printf("%d\n", countLargestGroup(2)); // expect: 2 +} diff --git a/count_largest_group.py b/count_largest_group.py new file mode 100644 index 0000000..3158100 --- /dev/null +++ b/count_largest_group.py @@ -0,0 +1,39 @@ +# 1399. Count Largest Group + +""" +you are given an integer 'n'. each number from 1 to 'n' is grouped according +to the sum of its digits. return the number of groups that have the largest +size. +""" + + +class Solution(object): + def get_num(self, n): + res = 0 + while n != 0: + res += n % 10 + n //= 10 + return res + + def countLargestGroup(self, n): + """ + :type n: int + :rtype: int + """ + arr, m = [0] * 37, 0 + for i in range(1, n + 1): + curr = self.get_num(i) + arr[curr] += 1 + if arr[curr] > m: + m = arr[curr] + cnt = 0 + for i in range(37): + if arr[i] == m: + cnt += 1 + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.countLargestGroup(n=13)) + print(obj.countLargestGroup(n=2)) |