Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 600 Non-negative Integers without Consecutive Ones - Problem Statement

LeetCode 600

Given a positive integer nn, return the number of non-negative integers n\leq n whose binary representation does not contain consecutive ones. For n=5n = 5 (binary 101101): valid numbers are 0,1,2,4,50, 1, 2, 4, 5 (binary 0,1,10,100,1010, 1, 10, 100, 101). Invalid: 33 (binary 1111).

Answer: 55. This is digit DP on binary representation. Instead of base 1010, you work in base 22. Before reading the solution, try to identify the base case and recursive relationship yourself.