2311. Longest Binary Subsequence Less Than or Equal to K Medium

@problem@discussion
#String#Dynamic Programming#Greedy#Memoization



1/**
2 * [2311] Longest Binary Subsequence Less Than or Equal to K
3 *
4 * You are given a binary string s and a positive integer k.
5 * Return the length of the longest subsequence of s that makes up a binary number less than or equal to k.
6 * Note:
7 * 
8 * 	The subsequence can contain leading zeroes.
9 * 	The empty string is considered to be equal to 0.
10 * 	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.
11 * 
12 *  
13 * Example 1:
14 * 
15 * Input: s = "1001010", k = 5
16 * Output: 5
17 * 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.
18 * Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
19 * The length of this subsequence is 5, so 5 is returned.
20 * 
21 * Example 2:
22 * 
23 * Input: s = "00101001", k = 1
24 * Output: 6
25 * 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.
26 * The length of this subsequence is 6, so 6 is returned.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= s.length <= 1000
32 * 	s[i] is either '0' or '1'.
33 * 	1 <= k <= 10^9
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/
39// discuss: https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn longest_subsequence(s: String, k: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_2311() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.