Skip to content

Commit 891c167

Browse files
authored
update: 添加问题“2311.小于等于K的最长二进制子序列”的代码和题解(#1001)
* archive: #1000 - 晚上回去换设备 (#1000) java - AC,83.64%,72.73% Signed-off-by: LetMeFly666 <[email protected]> * 2311: AC.cpp + WA.py (#1000) cpp - AC,4.00%,64.00% Signed-off-by: LetMeFly666 <[email protected]> * 2311: AC.py+ WA.go (#1000) py - AC,100.00%,45.10% Signed-off-by: LetMeFly666 <[email protected]> * 2311: AC.(cpp+py+go) + WA.java (#1000) 其实i<k_length的时候再判断就好,不需要等于的时候(例如k只有1个bit,那么也就i=0的时候值得判断一下) cpp - AC,小优化,100.00%,22.22% py - AC,29.41%,33.33% go - AC,100.00%,88.24% Signed-off-by: LetMeFly666 <[email protected]> * 2311: AC.java (#1000) java - AC,100.00%,65.71% Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2311.小于等于K的最长二进制子序列”的代码和题解(#1001) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 2733134 commit 891c167

12 files changed

+416
-95
lines changed

.changelog

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-26 22:16:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-26 22:43:01
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int longestSubsequence(string s, int k) {
14+
int ans = 0, val = 0, k_length = 32 - __builtin_clz(k), n = s.size(); // k=0时k_length=0计算错误也不受影响
15+
for (int i = 0; i < n; i++) {
16+
if (s[n - i - 1] == '0') {
17+
ans++;
18+
continue;
19+
}
20+
if (i < k_length && val + (1 << i) <= k) {
21+
val += 1 << i;
22+
ans++;
23+
}
24+
}
25+
return ans;
26+
}
27+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-26 22:16:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-26 22:39:55
6+
*/
7+
package main
8+
9+
import "math/bits"
10+
11+
func longestSubsequence(s string, k int) (ans int) {
12+
val, n, lenK := 0, len(s), bits.Len(uint(k))
13+
for i := 0; i < n; i++ {
14+
if s[n - i - 1] == '0' {
15+
ans++
16+
continue
17+
}
18+
if i < lenK && val + (1 << i) <= k {
19+
ans++
20+
val += 1 << i
21+
}
22+
}
23+
return
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-06-26 22:16:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-06-26 22:45:37
6+
*/
7+
class Solution {
8+
public int longestSubsequence(String s, int k) {
9+
int ans = 0, n = s.length(), lenK = 32 - Integer.numberOfLeadingZeros(k), val = 0;
10+
for (int i = 0; i < n; i++) {
11+
if (s.charAt(n - i - 1) == '0') {
12+
ans++;
13+
continue;
14+
}
15+
if (i < lenK && val + (1 << i) <= k) {
16+
val += 1 << i;
17+
ans++;
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-06-26 22:16:30
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-06-26 22:38:54
6+
'''
7+
class Solution:
8+
def longestSubsequence(self, s: str, k: int) -> int:
9+
ans, val, k_length = 0, 0, k.bit_length()
10+
for i, c in enumerate(s[::-1]):
11+
if c == '0':
12+
ans += 1
13+
continue
14+
if i < k_length and val + (1 << i) <= k:
15+
ans += 1
16+
val += 1 << i
17+
return ans

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
|Python - with语句 - 写一个支持with方法调用的类|<a href="https://blog.letmefly.xyz/2024/05/08/Other-Python-WithStatement-write1classSupportingWith/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/138576722">CSDN博客</a>|
122122
|QT - 实例 - 可点击的进度条、无窗口应用|<a href="https://blog.letmefly.xyz/2023/08/02/Other-QT-Example-ClickableProgressBar-noWindow/">本平台博客</a>|无|
123123
|北邮暑期课 - R语言数据分析|<a href="https://blog.letmefly.xyz/2023/06/26/Other-RLanguageDataAnalysis">本平台博客</a>|无|
124-
|域名转到国外注册商-备案实效风险折腾日记|<a href="https://blog.letmefly.xyz/2025/06/23/Other-Server-BeiAnXiaoTuCao/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/148844350">CSDN博客</a>|
124+
|域名转到国外注册商-备案失效风险折腾日记|<a href="https://blog.letmefly.xyz/2025/06/23/Other-Server-BeiAnXiaoTuCao/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/148844350">CSDN博客</a>|
125125
|SVG(可缩放矢量图形) - toLearn 和 创建|<a href="https://blog.letmefly.xyz/2023/07/06/Other-SVG-ToLearnAndToCreate/">本平台博客</a>|无|
126126
|Verilog学习笔记 - 极简极入门级|<a href="https://blog.letmefly.xyz/2023/01/06/Other-Verilog-Note/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/128584160">CSDN博客</a>|
127127
|Cloudflare Warp+,1.1.1.1,如何使用,如何获取免费流量,如何优选IP|<a href="https://blog.letmefly.xyz/2023/08/25/Other-VPN-CloudflareWarp+1.1.1.1">本平台博客</a>|无|
@@ -724,6 +724,7 @@
724724
|2303.计算应缴税款总额|简单|<a href="https://leetcode.cn/problems/calculate-amount-paid-in-taxes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/23/LeetCode%202303.%E8%AE%A1%E7%AE%97%E5%BA%94%E7%BC%B4%E7%A8%8E%E6%AC%BE%E6%80%BB%E9%A2%9D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128751683" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/calculate-amount-paid-in-taxes/solutions/2073612/letmefly-2303ji-suan-ying-jiao-shui-kuan-lzsi/" target="_blank">LeetCode题解</a>|
725725
|2304.网格中的最小路径代价|中等|<a href="https://leetcode.cn/problems/minimum-path-cost-in-a-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/22/LeetCode%202304.%E7%BD%91%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%AF%E5%BE%84%E4%BB%A3%E4%BB%B7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134563145" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-path-cost-in-a-grid/solutions/2537837/letmefly-2304wang-ge-zhong-de-zui-xiao-l-bjad/" target="_blank">LeetCode题解</a>|
726726
|2309.兼具大小写的最好英文字母|简单|<a href="https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/27/LeetCode%202309.%E5%85%BC%E5%85%B7%E5%A4%A7%E5%B0%8F%E5%86%99%E7%9A%84%E6%9C%80%E5%A5%BD%E8%8B%B1%E6%96%87%E5%AD%97%E6%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128769360" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/solutions/2077745/letmefly-2309jian-ju-da-xiao-xie-de-zui-3hgag/" target="_blank">LeetCode题解</a>|
727+
|2311.小于等于K的最长二进制子序列|中等|<a href="https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/26/LeetCode%202311.%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8EK%E7%9A%84%E6%9C%80%E9%95%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148935308" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/solutions/3709306/letmefly-2311xiao-yu-deng-yu-k-de-zui-ch-2n27/" target="_blank">LeetCode题解</a>|
727728
|2312.卖木头块|困难|<a href="https://leetcode.cn/problems/selling-pieces-of-wood/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/15/LeetCode%202312.%E5%8D%96%E6%9C%A8%E5%A4%B4%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136747100" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/selling-pieces-of-wood/solutions/2689067/letmefly-2312mai-mu-tou-kuai-dong-tai-gu-vk5a/" target="_blank">LeetCode题解</a>|
728729
|2316.统计无向图中无法互相到达点对数|中等|<a href="https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/21/LeetCode%202316.%E7%BB%9F%E8%AE%A1%E6%97%A0%E5%90%91%E5%9B%BE%E4%B8%AD%E6%97%A0%E6%B3%95%E4%BA%92%E7%9B%B8%E5%88%B0%E8%BE%BE%E7%82%B9%E5%AF%B9%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133962709" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/solutions/2492186/letmefly-2316tong-ji-wu-xiang-tu-zhong-w-79vz/" target="_blank">LeetCode题解</a>|
729730
|2335.装满杯子需要的最短总时长|简单|<a href="https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/02/11/LeetCode%202335.%E8%A3%85%E6%BB%A1%E6%9D%AF%E5%AD%90%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E7%9F%AD%E6%80%BB%E6%97%B6%E9%95%BF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128980819" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/solutions/2105156/letmefly-2335zhuang-man-bei-zi-xu-yao-de-kq65/" target="_blank">LeetCode题解</a>|
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: 2311.小于等于 K 的最长二进制子序列:贪心(先选0再选1)-好像还是比灵神写的清晰些
3+
date: 2025-06-26 22:46:22
4+
tags: [题解, LeetCode, 中等, 贪心, 字符串, 位运算]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2311.小于等于 K 的最长二进制子序列:贪心(先选0再选1)-好像还是比灵神写的清晰些
9+
10+
力扣题目链接:[https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/](https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/)
11+
12+
<p>给你一个二进制字符串&nbsp;<code>s</code>&nbsp;和一个正整数&nbsp;<code>k</code>&nbsp;。</p>
13+
14+
<p>请你返回 <code>s</code>&nbsp;的 <strong>最长</strong>&nbsp;子序列,且该子序列对应的 <strong>二进制</strong>&nbsp;数字小于等于 <code>k</code>&nbsp;。</p>
15+
16+
<p>注意:</p>
17+
18+
<ul>
19+
<li>子序列可以有 <strong>前导 0</strong>&nbsp;。</li>
20+
<li>空字符串视为&nbsp;<code>0</code>&nbsp;。</li>
21+
<li><strong>子序列</strong>&nbsp;是指从一个字符串中删除零个或者多个字符后,不改变顺序得到的剩余字符序列。</li>
22+
</ul>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre><b>输入:</b>s = "1001010", k = 5
29+
<b>输出:</b>5
30+
<b>解释:</b>s 中小于等于 5 的最长子序列是 "00010" ,对应的十进制数字是 2 。
31+
注意 "00100" 和 "00101" 也是可行的最长子序列,十进制分别对应 4 和 5 。
32+
最长子序列的长度为 5 ,所以返回 5 。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre><b>输入:</b>s = "00101001", k = 1
38+
<b>输出:</b>6
39+
<b>解释:</b>"000001" 是 s 中小于等于 1 的最长子序列,对应的十进制数字是 1 。
40+
最长子序列的长度为 6 ,所以返回 6 。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
49+
<li><code>s[i]</code> 要么是&nbsp;<code>'0'</code>&nbsp;,要么是&nbsp;<code>'1'</code> 。</li>
50+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法:贪心
56+
57+
二话不说选中所有0,接着从右往左开始选1,全程保证所选元素不超过$k$。
58+
59+
Why?
60+
61+
选1可能导致没法选后面的0,选0不会影响后面的选择。如果选1导致无法选后面的0,那么不如舍弃这个1直接替换成0。综上**先选0更优**
62+
63+
选1好说,当然是**从低位往高位选1**啦。
64+
65+
+ 时间复杂度$O(len(s))$
66+
+ 空间复杂度$O(1)$
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
/*
74+
* @Author: LetMeFly
75+
* @Date: 2025-06-26 22:16:30
76+
* @LastEditors: LetMeFly.xyz
77+
* @LastEditTime: 2025-06-26 22:43:01
78+
*/
79+
class Solution {
80+
public:
81+
int longestSubsequence(string s, int k) {
82+
int ans = 0, val = 0, k_length = 32 - __builtin_clz(k), n = s.size(); // k=0时k_length=0计算错误也不受影响
83+
for (int i = 0; i < n; i++) {
84+
if (s[n - i - 1] == '0') {
85+
ans++;
86+
continue;
87+
}
88+
if (i < k_length && val + (1 << i) <= k) {
89+
val += 1 << i;
90+
ans++;
91+
}
92+
}
93+
return ans;
94+
}
95+
};
96+
```
97+
98+
#### Python
99+
100+
```python
101+
'''
102+
Author: LetMeFly
103+
Date: 2025-06-26 22:16:30
104+
LastEditors: LetMeFly.xyz
105+
LastEditTime: 2025-06-26 22:38:54
106+
'''
107+
class Solution:
108+
def longestSubsequence(self, s: str, k: int) -> int:
109+
ans, val, k_length = 0, 0, k.bit_length()
110+
for i, c in enumerate(s[::-1]):
111+
if c == '0':
112+
ans += 1
113+
continue
114+
if i < k_length and val + (1 << i) <= k:
115+
ans += 1
116+
val += 1 << i
117+
return ans
118+
```
119+
120+
Python也可以理解为[依次删除左边1直到int(s)<=k](https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/solutions/3701151/xiao-yu-deng-yu-k-de-zui-chang-er-jin-zh-7vk8/comments/3064179/)
121+
122+
#### Java
123+
124+
```java
125+
/*
126+
* @Author: LetMeFly
127+
* @Date: 2025-06-26 22:16:30
128+
* @LastEditors: LetMeFly.xyz
129+
* @LastEditTime: 2025-06-26 22:45:37
130+
*/
131+
class Solution {
132+
public int longestSubsequence(String s, int k) {
133+
int ans = 0, n = s.length(), lenK = 32 - Integer.numberOfLeadingZeros(k), val = 0;
134+
for (int i = 0; i < n; i++) {
135+
if (s.charAt(n - i - 1) == '0') {
136+
ans++;
137+
continue;
138+
}
139+
if (i < lenK && val + (1 << i) <= k) {
140+
val += 1 << i;
141+
ans++;
142+
}
143+
}
144+
return ans;
145+
}
146+
}
147+
```
148+
149+
#### Go
150+
151+
```go
152+
/*
153+
* @Author: LetMeFly
154+
* @Date: 2025-06-26 22:16:30
155+
* @LastEditors: LetMeFly.xyz
156+
* @LastEditTime: 2025-06-26 22:39:55
157+
*/
158+
package main
159+
160+
import "math/bits"
161+
162+
func longestSubsequence(s string, k int) (ans int) {
163+
val, n, lenK := 0, len(s), bits.Len(uint(k))
164+
for i := 0; i < n; i++ {
165+
if s[n - i - 1] == '0' {
166+
ans++
167+
continue
168+
}
169+
if i < lenK && val + (1 << i) <= k {
170+
ans++
171+
val += 1 << i
172+
}
173+
}
174+
return
175+
}
176+
```
177+
178+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148935308)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/06/26/LeetCode%202311.%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8EK%E7%9A%84%E6%9C%80%E9%95%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%90%E5%BA%8F%E5%88%97/)哦~
179+
>
180+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)