Trains the technique from
LeetCode 338Counting BitsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A queue display shows a ticket id in binary on a row of lamps: a lamp is lit for each 1 in the binary form of the id, and dark for each 0. Ids run from 0 up to n inclusive.
Report the lit lamp count for each id in turn, starting at id 0 and finishing at id n, so the reported list holds one more entry than n. Id 0 lights nothing.
Aim for a running time linear in n, without a fresh scan of the bits of every id.
Example 1
Ids 0 to 9 in binary are 0, 1, 10, 11, 100, 101, 110, 111, 1000 and 1001, so the lit lamp counts run 0, 1, 1, 2, 1, 2, 2, 3, 1, 2.
Example 2
The counts for ids 0 to 9 are unchanged, and ids 10 to 13 are 1010, 1011, 1100 and 1101, lighting 2, 3, 2 and 3 lamps.
Example 3
Only id 0 exists and it lights no lamp, so the answer holds a single zero.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_bits(n: int) -> list[int]:public int[] countBits(int n)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.