Skip to content

Commit 1fb1c93

Browse files
committedMar 7, 2025
Add solution #600
1 parent 72c3ec8 commit 1fb1c93

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@
464464
594|[Longest Harmonious Subsequence](./0594-longest-harmonious-subsequence.js)|Easy|
465465
598|[Range Addition II](./0598-range-addition-ii.js)|Easy|
466466
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
467+
600|[Non-negative Integers without Consecutive Ones](./0600-non-negative-integers-without-consecutive-ones.js)|Hard|
467468
605|[Can Place Flowers](./0605-can-place-flowers.js)|Easy|
468469
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
469470
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 600. Non-negative Integers without Consecutive Ones
3+
* https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/
4+
* Difficulty: Hard
5+
*
6+
* Given a positive integer n, return the number of the integers in the range [0, n] whose
7+
* binary representations do not contain consecutive ones.
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var findIntegers = function(n) {
15+
const binary = n.toString(2);
16+
const dp = new Array(binary.length + 1).fill(0);
17+
let result = 0;
18+
19+
dp[0] = 1;
20+
dp[1] = 2;
21+
22+
for (let i = 2; i <= binary.length; i++) {
23+
dp[i] = dp[i - 1] + dp[i - 2];
24+
}
25+
26+
for (let i = 0, previous = 0; i < binary.length; i++) {
27+
if (binary[i] === '1') {
28+
result += dp[binary.length - i - 1];
29+
if (previous === 1) {
30+
return result;
31+
}
32+
previous = 1;
33+
} else {
34+
previous = 0;
35+
}
36+
}
37+
38+
return result + 1;
39+
};

0 commit comments

Comments
 (0)
Please sign in to comment.