当前位置:网站首页>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)
边栏推荐
- Android high frequency network interview topic must know and be able to compare Android development environment
- 321. Chessboard segmentation (2D interval DP)
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- Parted command
- There are spaces in the for loop variable in the shell -- IFS variable
- Redis环境搭建和使用的方法
- TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
- Learn about servlets
- D discard the virtual recovery method
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
猜你喜欢

并发编程的三大核心问题

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

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

golang---锁

leetcode2309. 兼具大小写的最好英文字母(简单,周赛)

matlab 实现语音信号重采样和归一化,并播放比对效果

MPLS experiment operation

Redis环境搭建和使用的方法

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages

MySQL中一条SQL是怎么执行的
随机推荐
MySQL中一条SQL是怎么执行的
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
【C#】使用正则校验内容
new和malloc的区别
Convolutional neural network (including code and corresponding diagram)
734. Energy stone (greed, backpack)
uTools
并发编程的三大核心问题
PR second training
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
Laravel artisan 常用命令
Four basic strategies for migrating cloud computing workloads
Self drawing of menu items and CListBox items
Based on configured schedule, the given trigger will never fire
Learn about servlets
The technology boss is ready, and the topic of position C is up to you
Selection of field types for creating tables in MySQL database
golang---锁
Using mongodb in laravel
Five skills of adding audio codec to embedded system