Given two non-negative integers n and k, return the number of binary strings of length n with at most k consecutive ones.
Example:
Input: n = 4, k = 2
Output: 13
Explanation: [
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101"
]
"0111", "1110" and "1111" are not valid because they have more than 2 consecutive ones
Notes:
k <= n <= 30
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
