当前位置:网站首页>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)
边栏推荐
- Using tabbar in wechat applet
- The difference between new and malloc
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- uTools
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- Raspberry pie 4B learning notes - IO communication (1-wire)
- Ks006 student achievement management system based on SSM
- Volume compression, decompression
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- 人工智能在网络安全中的作用
猜你喜欢
Implementation principle of city selector component
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Redis有序集合如何使用
[Floyd] post disaster reconstruction
KS006基于SSM实现学生成绩管理系统
人工智能在网络安全中的作用
【LeetCode 43】236. The nearest common ancestor of binary tree
479. Additive binary tree (interval DP on the tree)
leetcode373. 查找和最小的 K 对数字(中等)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
随机推荐
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
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
Have you stepped on the nine common pits in the e-commerce system?
电子协会 C语言 1级 32、计算2的幂
机器学习基本概念
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
734. Energy stone (greed, backpack)
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
PR second training
Electronic Association C language level 1 33, odd even number judgment
Architecture evolution from MVC to DDD
Android high frequency network interview topic must know and be able to compare Android development environment
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
ES6 new method of string
Medical management system (C language course for freshmen)
Four basic strategies for migrating cloud computing workloads
1069. Division of convex polygons (thinking, interval DP)
uTools
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking