2311. Longest Binary Subsequence Less Than or Equal to K

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2311

class Solution:
    def longestSubsequence(self, s: str, k: int) -> int:
        res = v = 0
        for c in s[::-1]:
            if c == "0":
                res += 1
            elif res < 30 and (v | 1 << res) <= k:
                v |= 1 << res
                res += 1
        return res