返回介绍

solution / 2300-2399 / 2311.Longest Binary Subsequence Less Than or Equal to K / README_EN

发布于 2024-06-17 01:03:07 字数 4865 浏览 0 评论 0 收藏 0

2311. Longest Binary Subsequence Less Than or Equal to K

中文文档

Description

You are given a binary string s and a positive integer k.

Return _the length of the longest subsequence of _s_ that makes up a binary number less than or equal to_ k.

Note:

  • The subsequence can contain leading zeroes.
  • The empty string is considered to be equal to 0.
  • A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

 

Example 1:

Input: s = "1001010", k = 5
Output: 5
Explanation: The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
The length of this subsequence is 5, so 5 is returned.

Example 2:

Input: s = "00101001", k = 1
Output: 6
Explanation: "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
The length of this subsequence is 6, so 6 is returned.

 

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either '0' or '1'.
  • 1 <= k <= 109

Solutions

Solution 1

class Solution:
  def longestSubsequence(self, s: str, k: int) -> int:
    ans = v = 0
    for c in s[::-1]:
      if c == "0":
        ans += 1
      elif ans < 30 and (v | 1 << ans) <= k:
        v |= 1 << ans
        ans += 1
    return ans
class Solution {
  public int longestSubsequence(String s, int k) {
    int ans = 0, v = 0;
    for (int i = s.length() - 1; i >= 0; --i) {
      if (s.charAt(i) == '0') {
        ++ans;
      } else if (ans < 30 && (v | 1 << ans) <= k) {
        v |= 1 << ans;
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int longestSubsequence(string s, int k) {
    int ans = 0, v = 0;
    for (int i = s.size() - 1; ~i; --i) {
      if (s[i] == '0') {
        ++ans;
      } else if (ans < 30 && (v | 1 << ans) <= k) {
        v |= 1 << ans;
        ++ans;
      }
    }
    return ans;
  }
};
func longestSubsequence(s string, k int) (ans int) {
  for i, v := len(s)-1, 0; i >= 0; i-- {
    if s[i] == '0' {
      ans++
    } else if ans < 30 && (v|1<<ans) <= k {
      v |= 1 << ans
      ans++
    }
  }
  return
}
function longestSubsequence(s: string, k: number): number {
  let ans = 0;
  for (let i = s.length - 1, v = 0; ~i; --i) {
    if (s[i] == '0') {
      ++ans;
    } else if (ans < 30 && (v | (1 << ans)) <= k) {
      v |= 1 << ans;
      ++ans;
    }
  }
  return ans;
}
/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var longestSubsequence = function (s, k) {
  let ans = 0;
  for (let i = s.length - 1, v = 0; ~i; --i) {
    if (s[i] == '0') {
      ++ans;
    } else if (ans < 30 && (v | (1 << ans)) <= k) {
      v |= 1 << ans;
      ++ans;
    }
  }
  return ans;
};
public class Solution {
  public int LongestSubsequence(string s, int k) {
    int ans = 0, v = 0;
    for (int i = s.Length - 1; i >= 0; --i) {
      if (s[i] == '0') {
        ++ans;
      } else if (ans < 30 && (v | 1 << ans) <= k) {
        v |= 1 << ans;
        ++ans;
      }
    }
    return ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文