LeetCode solutions

1844. Maximum Number of Balls in a Box

My accepted Python solution to LeetCode problem 1844, Maximum Number of Balls in a Box, running in 204ms.

  • Difficulty: Easy
  • Python
  • Runtime 204ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 204ms, memory 17.3MB, accepted 2025-12-31.

python
class Solution:
    def countBalls(self, lowLimit: int, highLimit: int) -> int:
        from collections import Counter
        
        def digit_sum(n):
            total = 0
            while n:
                total += n % 10
                n //= 10
            return total
        
        boxes = Counter(digit_sum(i) for i in range(lowLimit, highLimit + 1))
        return max(boxes.values())

Source