Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dee777f

Browse files
authoredJul 27, 2021
Create 600. Non-negative Integers without Consecutive Ones
1 parent 497f8be commit dee777f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
static int[] dp;
3+
public int findIntegers(int n) {
4+
int ones = helper(n);
5+
return n - ones + 1;
6+
}
7+
8+
int helper(int n){
9+
double pow = Math.log(n)/Math.log(2);
10+
if(pow == (int)pow) return dp[(int)pow];
11+
pow = Math.floor(pow);
12+
int ans = dp[(int) pow];
13+
14+
int lowestNumber = 1<<(int)pow;
15+
16+
int left = n - lowestNumber;
17+
18+
if(left>=lowestNumber/2) ans+= dp[(int)pow - 1] + (left - lowestNumber/2 + 1);
19+
else ans+=helper(left);
20+
return ans;
21+
}
22+
23+
static{
24+
dp = new int[33];
25+
dp[0] = 0;
26+
dp[1] = 0;
27+
int val = 1;
28+
for(int i=2;i<33;i++){
29+
dp[i] = dp[i-1] + dp[i-2] + val;
30+
val=val<<1;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.