Problem overview
The method countBits takes an integer n and returns a vector<int> of n + 1 numbers. Entry i is the number of 1 bits in the binary form of i, for every i from 0 to n.
For n = 5 the numbers 0 to 5 are 0, 1, 10, 11, 100, 101 in binary, so the answer is [0, 1, 1, 2, 1, 2]. For n = 0 the answer is [0].
The statement asks for O(n) time, a constant amount of work per number, and no extra space besides the returned vector.
Approach: reuse the count of a smaller number
Counting the bits of every number separately costs up to about 17 steps each for values up to 100,000, and it ignores a pattern: the answers are related. Shifting a number right by one place drops its last bit. So i has the same 1 bits as i >> 1, plus its own last bit, which is i & 1: 1 for an odd number and 0 for an even one.
That gives the recurrence bits[i] = bits[i >> 1] + (i & 1). Since i >> 1 is smaller than i, its count is already in the vector when we reach i, as long as we fill the vector from 0 upward. bits[0] is 0 and needs no work.
For example, 13 is 1101. Shifted right it is 110, which is 6, with two 1 bits. The last bit of 13 is 1, so 13 has three. The lesson's code for n = 13 returns:
0 1 1 2 1 2 2 3 1 2 2 3 2 3
The entry at index 6 is 2 and the entry at index 13 is 3, as computed above.
Step by step
- Create
bitswithn + 1zeros: one slot for each number from 0 ton. Zero has no 1 bits, sobits[0]is already right. - Loop from 1 to
ninclusive. The order is upward, so each number finds the smaller one's answer already stored. - Apply the recurrence:
bits[i] = bits[i >> 1] + (i & 1). - Return
bits.
The solution in C++
class Solution {
public:
vector<int> countBits(int n) {
vector<int> bits(n + 1, 0);
for (int i = 1; i <= n; i++) {
bits[i] = bits[i >> 1] + (i & 1);
}
return bits;
}
};
C++ details worth knowing:
- Size and value.
vector<int> bits(n + 1, 0)makesn + 1elements, each 0. Valid indices run from 0 ton, so the loop condition isi <= n, noti < n. - Parentheses around
i & 1. The operator&binds more loosely than+, sobits[i >> 1] + i & 1would mean(bits[i >> 1] + i) & 1, a different number. >>on a non-negative int divides by 2 and rounds down, soi >> 1is the same asi / 2here.- Returning by value. Returning the local vector doesn't copy it element by element; the compiler moves it or builds it in place.
Complexity
Time: O(n). The loop runs n times and each pass does a shift, an AND, an addition and one lookup.
Space: O(1) extra. Apart from the vector that is returned, only the loop variable is kept.
Edge cases and common mistakes
n = 0. The vector has one element, 0, and the loop doesn't run. The answer is[0].- Stopping one short. With
i < nthe last entry stays 0. Forn = 5that would give[0, 1, 1, 2, 1, 0]. - Filling downward. The recurrence needs the smaller number's answer first, so the loop must go upward.
- Forgetting the last bit.
bits[i >> 1]alone is off by one for every oddi.
Testing
n = 2gives[0, 1, 1].n = 5gives[0, 1, 1, 2, 1, 2].n = 0gives[0].n = 13gives[0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3].
