<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 f(int i, int prev, bool flag, vector<int>& nums, vector<vector<vector<int>>>& dp){
        int n=nums.size();
        if(i>=n){
            return 1;
        }
        if(dp[i][prev][flag] != -1){
            return dp[i][prev][flag];
        }

        int ans = 0;
        bool newFlag = flag;
        if(nums[i] == 1){
            if(prev != 1){
                ans += f(i+1, 1, newFlag, nums, dp);
            }
            ans+=f(i+1, 0, false, nums, dp);
        }
        else{
            if(prev != 1 && newFlag == false){
                ans += f(i+1, 1, false, nums, dp);
            }
            ans+=f(i+1, 0, newFlag, nums, dp);
        }

        return dp[i][prev][flag] = ans;
    }
    int findIntegers(int n) {
        vector<int> nums;
        while(n){
            nums.push_back(n&1);
            n = n>>1;
        }

        reverse(nums.begin(), nums.end());
        int size=nums.size();
        vector<vector<vector<int>>> dp(size, vector<vector<int>> (2, vector<int> (2, -1)));
        return f(0, 0, true, nums, dp);
    }
};
11600