2025-08-15 11:17:27 +0000 UTC

Maximum Number of Balls in a Box

Code

class Solution:
    def countBalls(self, lowLimit: int, highLimit: int) -> int:
        freqs = [0] * 100
        max_freq = 0
        for num in range(lowLimit, highLimit + 1):
            box = 0
            while num > 0:
                box += num % 10
                num //= 10
            freqs[box] += 1
            max_freq = max(max_freq, freqs[box])
        return max_freq