The task requests bit counts for every integer in an increasing range, so each answer can reuse a smaller already-computed integer instead of recounting from scratch.
Keep true
Before computing answer[value], all lower indices are correct. Because value >> 1 is smaller, its count is available, and value & 1 contributes exactly the removed low bit.
Reuse it
When a bit operation maps each state to a smaller state, build answers in numeric order and append the removed-bit contribution; similar recurrences use x & (x - 1), highest powers of two, or parity.
Read it this way: For limit 6, allocate seven cells. Zero has no set bits, so answer[0] = 0 before the loop starts. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: DP from a number with its last bit removed.
Simple idea:value >> 1 is the same number without its last bit. Add that last bit to
the saved answer.
def count_bits(limit: int) -> list[int]: answer = [0] * (limit + 1) for value in range(1, limit + 1): answer[value] = answer[value >> 1] + (value & 1) return answer