Skip to content

Commit 99a3fbd

Browse files
committed
Jun 26
1 parent 7742b0f commit 99a3fbd

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def longestSubsequence(self, s: str, k: int) -> int:
3+
max_len, curr_sum = 0, 0
4+
for d in reversed(s):
5+
if d == '0':
6+
max_len += 1
7+
elif curr_sum + (1 << max_len) <= k:
8+
curr_sum += (1 << max_len)
9+
max_len += 1
10+
return max_len
11+
12+
13+
def main():
14+
s = '1001010'
15+
k = 5
16+
assert Solution().longestSubsequence(s, k) == 5
17+
18+
s = '00101001'
19+
k = 1
20+
assert Solution().longestSubsequence(s, k) == 6
21+
22+
23+
if __name__ == '__main__':
24+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
| June 22 | [2138. Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/) | Easy | Solved |
2929
| June 23 | [2081. Sum of k-Mirror Numbers](https://leetcode.com/problems/sum-of-k-mirror-numbers/) | Hard | Unsolved |
3030
| June 24 | [2200. Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/) | Easy | Solved |
31-
| June 25 | []() | | |
32-
| June 26 | []() | | |
31+
| June 25 | [2040. Kth Smallest Product of Two Sorted Arrays](https://leetcode.com/problems/kth-smallest-product-of-two-sorted-arrays/) | Hard | Unsolved |
32+
| June 26 | [2311. Longest Binary Subsequence Less Than or Equal to K](https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/) | Medium | Unsolved |
3333
| June 27 | []() | | |
3434
| June 28 | []() | | |
3535
| June 29 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 4 | 4 | 0 |
43-
| Medium | 10 | 7 | 3 |
44-
| Hard | 6 | 2 | 4 |
43+
| Medium | 11 | 7 | 4 |
44+
| Hard | 7 | 2 | 5 |

0 commit comments

Comments
 (0)