当前位置:网站首页>leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
2022-07-02 01:54:00 【Heavy garbage】



Ideas : greedy
Specific details : Want to find less than k The youngest sequence of , Therefore, the thought is to supplement as much as possible in the front 0, Greedy thoughts , hypothesis k Yes m position , Take the last m position /m-1 position , Plus all the previous 0, That is to say ans
class Solution {
public:
int longestSubsequence(string s, int k) {
int bits = 32 - __builtin_clz(k), n = s.size();
if (n < bits) return n;
int m = bits - 1;
string tmp = s.substr(n - bits);
if (stoi(tmp, nullptr, 2) <= k) m = bits;
return m + count_if(s.begin(), s.end() - m, [](char ch){
return ch == '0';});
}
};
Code difficulties :string Turn according to binary int :stoi(s, nullptr, 2)
边栏推荐
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- Parted command
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- Four basic strategies for migrating cloud computing workloads
- What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
- Learn about servlets
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- Penser au jeu 15: penser au service complet et au sous - service
- Raspberry pie 4B learning notes - IO communication (1-wire)
- [Floyd] post disaster reconstruction
猜你喜欢

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories

Opengauss database backup and recovery guide

leetcode373. 查找和最小的 K 对数字(中等)

如何远程、在线调试app?
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

Data analysis on the disaster of Titanic

Implementation of Weibo system based on SSM

Android high frequency network interview topic must know and be able to compare Android development environment

matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
![[question] - why is optical flow not good for static scenes](/img/8d/2cf6f582bc58cc2985f50e3f85f334.jpg)
[question] - why is optical flow not good for static scenes
随机推荐
人工智能在网络安全中的作用
MySQL中一条SQL是怎么执行的
跨域?同源?一次搞懂什么是跨域
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
How to debug apps remotely and online?
1218 square or round
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Discussion on the idea of platform construction
Failed to transform file 'xxx' to match attributes
MySQL约束与多表查询实例分析
Niuke - Huawei question bank (51~60)
Opengauss database backup and recovery guide
Design and implementation of key value storage engine based on LSM tree
What is AQS and its principle
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
734. Energy stone (greed, backpack)
5g/4g pole gateway_ Smart pole gateway