<p>Given a positive integer <code>n</code>, return the number of the integers in the range <code>[0, n]</code> whose binary representations <strong>do not</strong> contain consecutive ones.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong>
Here are the non-negative integers &lt;= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:

    int solve(int index, bool prev_one, bool already_smaller, const vector<bool>& bits, vector<vector<vector<int>>>& dp) {
        if (index >= dp[0][0].size()) return 1;
        if (dp[prev_one][already_smaller][index] >= 0) return dp[prev_one][already_smaller][index];
        int sum = 0;
        // Put zero
        sum += solve(index + 1, false, already_smaller | bits[index], bits, dp);
        // Put one
        if (
            // consecutive one condition
            !prev_one &&
            // smaller condition
            (already_smaller || bits[index])
        ) {
            sum += solve(index + 1, true, already_smaller, bits, dp);
        }
        return dp[prev_one][already_smaller][index] = sum;
    }

    int findIntegers(int n) {
        vector<bool> bits;
        while (n) {
            bits.push_back(n & 1);
            n >>= 1;
        }
        reverse(bits.begin(), bits.end());
        vector<vector<vector<int>>> dp(2, vector<vector<int>>(2, vector<int>(bits.size(), -1)));
        return solve(0, 0, 0, bits, dp);
    }
};
10300