Skip to content

Commit 52d11dd

Browse files
authored
Create 2311. Longest Binary Subsequence Less Than or Equal to K (#825)
2 parents 01c0cbb + 5f9b87c commit 52d11dd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int longestSubsequence(string s, int k) {
4+
int res = 0;
5+
long long cur = 0;
6+
for(int i = s.size() - 1; i >= 0; --i) {
7+
char c = s[i];
8+
if(c == '0') {
9+
res++;
10+
} else if(res < 31 && cur + (1LL << res) <= k) {
11+
cur += 1LL << res;
12+
res++;
13+
}
14+
}
15+
return res;
16+
}
17+
};

0 commit comments

Comments
 (0)