<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:
    vector<vector<vector<int>>> dp;  // Declare DP as a class member

    int solve(int &n, int pos, int tight, int crr) {
        if (pos >= 32) {
            return 1;  // Base case: if we have processed all 32 bits
        }
        
        if (dp[pos][tight][crr] != -1) {
            return dp[pos][tight][crr];  // Return memoized result
        }

        int ub = tight ? (n >> (31 - pos)) & 1 : 1;  // Calculate the upper bound based on the tight constraint
        int res = 0;

        // Iterate over possible bit values (0 or 1) up to the upper bound (ub)
        for (int i = 0; i <= ub; i++) {
            if (crr == 1 && i == 1) {
                continue;  // Skip if consecutive 1's would be formed
            }
            // Recursively solve for the next bit, update the tight condition, and whether the current bit is 1
            res += solve(n, pos + 1, tight & (i == ub), i == 1);
        }

        return dp[pos][tight][crr] = res;  // Memoize and return result
    }

    int findIntegers(int n) {
        // Resize DP table to 32 x 2 x 2 with -1 as initial values (for memoization)
        dp.resize(33, vector<vector<int>>(2, vector<int>(2, -1)));
        
        return solve(n, 0, 1, 0);  // Start solving with initial position, tight constraint, and no preceding '1'
    }
};

11800