From 5ac705b1894f3c85f6736c62cd5c0e44f254736e Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 23 Apr 2025 00:22:32 -0600 Subject: 1399. count largest group --- count_largest_group.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 count_largest_group.py (limited to 'count_largest_group.py') 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)) -- cgit v1.2.3