1399. Count-Largest-Group

Question Link

Difficulty: Easy

This took me way too long or wha-

This is a very brute force method waowaoaowoaowao. Just go through all of the digits from 1 to n and keep track of a counter for the sum of digits. Then we can iterate through the counter and count the number of entries with the largest value.

We return this count.

class Solution(object):
    def countLargestGroup(self, n):
        """
        :type n: int
        :rtype: int
        """
        hashmap = {}

        for i in range(1, n + 1):
            string = str(i)
            c = 0
            for digit in string:
                c += int(digit)

            hashmap[c] = hashmap.get(c, 0) + 1

        maximum = max(hashmap.values())
        count = 0
        for v in hashmap.values():
            if v == maximum:
                count += 1

        return count

Time Complexity: O(n)

Space Complexity: O(n)

Time Taken: 16m 50s