Skip to content

Commit b0feeba

Browse files
committed
Runtime: 2 ms (Top 83.60%) | Memory: 42.5 MB (Top 46.76%)
1 parent a5ea313 commit b0feeba

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
// Runtime: 2 ms (Top 83.60%) | Memory: 42.5 MB (Top 46.76%)
12
class Solution {
23
public int longestSubsequence(String s, int k) {
34
int z=0;
45
//count zero
56
for(int i=0;i<s.length();i++)if(s.charAt(i)=='0')z++;
67

78
int num=0,base=1,len=0;
8-
//take as many ones from right as possible until they do not make the num>k
9+
//take as many ones from right as possible until they do not make the num>k
910
for(int i=s.length()-1;i>=0;i--){
10-
11+
1112
if(num+base>k)break;
1213
if(s.charAt(i)=='1'){
1314
num+=base;
1415
}
1516
else {
16-
//remove already taken zeros from zeros count
17+
//remove already taken zeros from zeros count
1718
z--;
1819
}
1920
base*=2;
2021
len++;
2122
}
22-
23+
2324
return len+z;
2425
}
25-
}
26+
}

0 commit comments

Comments
 (0)