1 2 3 4 5 6 7 8 9
| class Solution { public: int longestSubsequence(string s, int k) { int n = s.size(), m = 32 - __builtin_clz(k); if (n < m) return n; int ans = stoi(s.substr(n - m), nullptr, 2) <= k ? m : m - 1; return ans + count(s.begin(), s.end() - m, '0'); } };
|