-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2311_longestBinarySubsequence.py
More file actions
39 lines (32 loc) · 1.29 KB
/
2311_longestBinarySubsequence.py
File metadata and controls
39 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Submitted solution to problem 2311. Longest Binary Subsequence Less Than or Equal to K
# You are given a binary string s and a positive integer k.
# Return the length of the longest subsequence of s that makes up a binary number less than or equal to k.
# Note:
# - The subsequence can contain leading zeroes.
# - The empty string is considered to be equal to 0.
# - A subsequence is a string that can be derived from another string by deleting some or no
# characters without changing the order of the remaining characters.
# Devnotes:
# As we can delete 1s, we will move from the right side and ignore all 1s that put our value above k
def longestSubsequence(s, k):
numOnes = 0
decValue = 0
exponent = 1
for i, bin in enumerate(s[::-1]):
if decValue + exponent > k:
break
elif bin == '1':
numOnes += 1
decValue += exponent
exponent *= 2
return s.count('0') + numOnes # Return all 0s, plus amount of 1s we traversed from the right while remaining under k
# Test cases:
testS1 = "000000001"
testK1 = 4
testS2 = "1001001110101"
testK2 = 2
testS3 = "1001010" # Leetcode example. Soltion = 5, s.t. k = 5
testK3 = 5
testS4 = "00101001" # Leetcode example. Solution = 6, s.t. k = 1
testK4 = 1
print(longestSubsequence(testS4, testK4))