File: maximum-number-of-balls-in-a-box/solution.py

Date: 2026-06-06

Time: 17:39

Purpose

This file is the self-contained solution and test suite for LeetCode 1742 — Maximum Number of Balls in a Box. It owns the implementation, the unit tests, and (unusually) an alias to satisfy a mismatched task spec. Like every other problem directory in this repo, it follows the solution.py + test_solution.py convention, though here both live in a single file.

Key Components

Solution.countBalls(lowLimit, highLimit) -> int

The core method. Given a range [lowLimit, highLimit], it assigns each ball numbered i to the box whose label equals the digit sum of i, then returns the count of the most populated box.

The implementation is a single expression: build a Counter over digit sums, then take the max of its values.

maxWidthOfVerticalArea (alias)

A class-level alias pointing to countBalls. The comment says it exists because the task spec expected a different method name — likely a copy-paste artifact from LeetCode's code template for a different problem (1637 — Widest Vertical Area). This means Solution().maxWidthOfVerticalArea(1, 10) calls the same function.

TestCountBalls

Six tests covering:

Patterns

Counter-based frequency analysis — the canonical Python idiom for "count occurrences, find the max." The generator expression inside Counter(...) avoids materializing an intermediate list.

Digit-sum via string conversionsum(int(d) for d in str(i)) converts the integer to a string, iterates characters, casts back. Readable but slower than repeated divmod. For LeetCode constraints (highLimit <= 100000) it's fine.

Inline test suite — tests live in the same file behind if _name == "main_", so python solution.py runs them directly.

Dependencies

Imports: collections.Counter (frequency counting), unittest (test harness).

Imported by: The testsolution.py files listed in the Imported By section don't actually import *this* file — that list appears to be a repo-wide cross-reference artifact showing all test files that follow the same pattern, not true reverse dependencies. The real consumer is maximum-number-of-balls-in-a-box/testsolution.py.

Flow

1. Iterate every integer i in [lowLimit, highLimit].

2. For each i, convert to string, sum the digit characters cast to int → this is the box number.

3. Feed all box numbers into Counter to get {box_number: count}.

4. Return max(counts.values()).

Time: O(n * d) where n = highLimit - lowLimit + 1 and d = number of digits (~5 at most). Space: O(k) where k = number of distinct digit sums (at most 45 for 5-digit numbers).

Invariants

Error Handling

None. Invalid inputs (e.g., lowLimit > highLimit) would produce an empty Counter, and max() on an empty sequence would raise ValueError. This is acceptable — LeetCode guarantees valid inputs.