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:
0.
Example 1:
Input: s = "1001010", k = 5 Output: 5 Explanation: The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal. Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively. The length of this subsequence is 5, so 5 is returned.
Example 2:
Input: s = "00101001", k = 1 Output: 6 Explanation: "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal. The length of this subsequence is 6, so 6 is returned.
Constraints:
1 <= s.length <= 1000s[i] is either '0' or '1'.1 <= k <= 109In this approach, we will aim to maximize the count of '0's in the subsequence because they contribute a value of 0 to the binary number, ensuring it stays small. We then try to add as many '1's as we can while ensuring that the binary number formed remains less than or equal to k.
This implementation iterates from the least significant to the most significant bit of the string. It initially prioritizes adding '0's because they do not impact the current numerical value 'value'. For each '1' encountered, a check is performed to see if adding this '1', would result in a binary number less than or equal to k. If it does, the '1' is accepted, otherwise, it is skipped.
C++
Java
Python
C#
JavaScript
The time complexity is O(n), where n is the length of the binary string, since we iterate through the string once. The space complexity is O(1) since only a few variables are used.
This approach leverages dynamic programming to store intermediate results and build up to the final output. It is generally less efficient than the greedy approach but illustrates a bottom-up method of solving the problem.
This method maintains a dynamic programming table where dp[i] corresponds to the longest subsequence from the start collapsing to i. For each '0', it elongates the subsistence without consideration of numerical limits, checking each '1' iteratively against increasing binary weights and augmenting upon acceptance.
C++
Java
Python
C#
JavaScript
The time complexity leverage here is O(n^2) due to dual allocation iterations over i and n bits, with space complexity as O(n) for intermediate storages.
| Approach | Complexity |
|---|---|
| Greedy Approach | The time complexity is O(n), where n is the length of the binary string, since we iterate through the string once. The space complexity is O(1) since only a few variables are used. |
| Dynamic Programming Approach (Less Optimal) | The time complexity leverage here is O(n^2) due to dual allocation iterations over i and n bits, with space complexity as O(n) for intermediate storages. |
Longest Increasing Subsequence - Dynamic Programming - Leetcode 300 • NeetCode • 432,482 views views
Watch 9 more video solutions →Practice Longest Binary Subsequence Less Than or Equal to K with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor