Skip to content

Commit 927294e

Browse files
Added task 2311.
1 parent f19b8d3 commit 927294e

File tree

3 files changed

+80
-0
lines changed
  • src

3 files changed

+80
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2301_2400.s2311_longest_binary_subsequence_less_than_or_equal_to_k;
2+
3+
// #Medium #Math #2022_06_20_Time_1_ms_(100.00%)_Space_42.3_MB_(50.00%)
4+
5+
public class Solution {
6+
public int longestSubsequence(String s, int k) {
7+
int res = 0;
8+
int cost = 1;
9+
int n = s.length();
10+
for (int i = n - 1; i >= 0; i--) {
11+
if (s.charAt(i) == '0' || cost <= k) {
12+
k -= cost * (s.charAt(i) - '0');
13+
++res;
14+
}
15+
if (cost <= k) {
16+
cost *= 2;
17+
}
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2311\. Longest Binary Subsequence Less Than or Equal to K
2+
3+
Medium
4+
5+
You are given a binary string `s` and a positive integer `k`.
6+
7+
Return _the length of the **longest** subsequence of_ `s` _that makes up a **binary** number less than or equal to_ `k`.
8+
9+
Note:
10+
11+
* The subsequence can contain **leading zeroes**.
12+
* The empty string is considered to be equal to `0`.
13+
* 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.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "1001010", k = 5
18+
19+
**Output:** 5
20+
21+
**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.
22+
23+
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
24+
25+
The length of this subsequence is 5, so 5 is returned.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "00101001", k = 1
30+
31+
**Output:** 6
32+
33+
**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.
34+
35+
The length of this subsequence is 6, so 6 is returned.
36+
37+
**Constraints:**
38+
39+
* `1 <= s.length <= 1000`
40+
* `s[i]` is either `'0'` or `'1'`.
41+
* <code>1 <= k <= 10<sup>9</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2311_longest_binary_subsequence_less_than_or_equal_to_k;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void longestSubsequence() {
11+
assertThat(new Solution().longestSubsequence("1001010", 5), equalTo(5));
12+
}
13+
14+
@Test
15+
void longestSubsequence2() {
16+
assertThat(new Solution().longestSubsequence("00101001", 1), equalTo(6));
17+
}
18+
}

0 commit comments

Comments
 (0)