600. Non-negative Integers without Consecutive Ones

Problem statement:

Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones.

Example 1:

Input: 5
Output: 5
Explanation:
Here are the non-negative integers <= 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. 

Note: 1 <= n <= 10^9

Solution:

This is the last question of leetcode weekly contest 34. It is a DP and definitely is hard problem. But, the DP formula is not difficult to figure it out. What we need to be careful is there is one requirement in the description: less than or equal  to n.  After we get the answer, we still need to do one more step.

Generally, this problem can be divided into several steps:

(1) Convert the original n into a binary representation string. Get the size of string to allocate memory for DP array.

(2)Do DP, like 198. House Robber, we should define two dp arrays:

  • dp0[i]: the number of integers when current bit set to 0
  • dp1[i]: the number of integers when current bit set to 1

(3)Any integer can not contain any consecutive ones.

  • dp0[i] = dp1[i - 1] + dp0[i - 1]
  • dp1[i] = dp0[i - 1]

(4) And do the last processing to find the integers which are less than or equal to n.

Time complexity is O(k), k is the bit count of n.

class Solution {
public:
    int findIntegers(int num) {
        string str_num;
        while(num){
            str_num.push_back(num % 2 + ‘0‘);
            num /= 2;
        }
        int size = str_num.size();
        vector<int> dp0(size, 0);
        vector<int> dp1(size, 0);
        dp0[0] = 1;
        dp1[0] = 1;
        for(int i = 1; i < size; i++){
            dp0[i] = dp0[i - 1] + dp1[i - 1];
            dp1[i] = dp0[i - 1];
        }
        int cnt = dp0[size - 1] + dp1[size - 1];
        for (int i = size - 2; i >= 0; i--) {
            if (str_num[i] == ‘1‘ && str_num[i + 1] == ‘1‘) {
                break;
            }
            if (str_num[i] == ‘0‘ && str_num[i + 1] == ‘0‘) {
                cnt -= dp1[i];
            }
        }
        return cnt;
    }
};
时间: 2024-10-06 11:56:14

600. Non-negative Integers without Consecutive Ones的相关文章

Non-negative Integers without Consecutive Ones

n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/ 也可以令f[i],表示共i位的二进制数字中,不包含连续1的二进制(i位)数字的个数. f[i]的组成有两部分: 最高位为1:只有当次高位为0,且满足条件的二进制数字个数,即 f[i-2] 最高位为0:次高位为0或1且满足条件的数字个数,即f[i-1] 得: f[i] = f[i-2]

第十六周 Leetcode 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp

Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示到第i位 j=0表示第i位为0 且值等于n的情况 2为值小于n的情况 j=1表示第i位为1 且值等于n的情况 3为值小于n的情况 转移方程很简单 看代码吧 这道题应该是Mid难度吧 class Solution { public: int findIntegers(int num) { int va

[LeetCode] Non-negative Integers without Consecutive Ones 非负整数不包括连续的1

Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones. Example 1: Input: 5 Output: 5 Explanation: Here are the non-negative integers <= 5 with their

[Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix

Each row and each column are already SORTED in the given matrix! const mix = [[-3, -2, -1, 3], [-1, 0, 1, 3], [0, 2, 4, 5]]; /** * Start from top right slot, go from right to left, top to bottom * case 1; If the current value is larger than 0, keep m

【LeetCode】动态规划(下篇)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】动态规划(下篇共39题)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

Interleaving Positive and Negative Numbers

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note: You are not necessary to keep the original order of positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6]

LintCode-Interleaving Positive and Negative Numbers.

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note You are not necessary to keep the original order or positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6],