Popcount array
Given a non-negative integer n, return a list of length n+1 where entry i is the number of set bits (popcount) in i, for i from 0 to n. You must compute it in O(n) total using the DP relation bits[i] = bits[i >> 1] + (i & 1), not by counting each number independently. Constraints: 0 <= n <= 100000.
Implement
counting_bits(n: int) → list[int]Examples
in
[5]out[0,1,1,2,1,2]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 20 min
solution.py
InputExpectedGot
[5][0,1,1,2,1,2]not run yetsample