Given an integer n, return an array ans of length n+1 such that for each i (0 ≤ i ≤ n), ans[i] is the number of 1's in the binary representation of i.
n = 2[0,1,1]n = 5[0,1,1,2,1,2]Every number i can be formed by taking i>>1 (dropping the last bit) and optionally adding 1 (the last bit i&1). Since i>>1 < i, dp[i>>1] is always already computed. This gives us a perfect O(1) recurrence per element — no counting, no loops, just table lookups.