返回介绍

solution / 0900-0999 / 0926.Flip String to Monotone Increasing / README_EN

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

926. Flip String to Monotone Increasing

中文文档

Description

A binary string is monotone increasing if it consists of some number of 0's (possibly none), followed by some number of 1's (also possibly none).

You are given a binary string s. You can flip s[i] changing it from 0 to 1 or from 1 to 0.

Return _the minimum number of flips to make _s_ monotone increasing_.

 

Example 1:

Input: s = "00110"
Output: 1
Explanation: We flip the last digit to get 00111.

Example 2:

Input: s = "010110"
Output: 2
Explanation: We flip to get 011111, or alternatively 000111.

Example 3:

Input: s = "00011000"
Output: 2
Explanation: We flip to get 00000000.

 

Constraints:

  • 1 <= s.length <= 105
  • s[i] is either '0' or '1'.

Solutions

Solution 1

class Solution:
  def minFlipsMonoIncr(self, s: str) -> int:
    n = len(s)
    left, right = [0] * (n + 1), [0] * (n + 1)
    ans = 0x3F3F3F3F
    for i in range(1, n + 1):
      left[i] = left[i - 1] + (1 if s[i - 1] == '1' else 0)
    for i in range(n - 1, -1, -1):
      right[i] = right[i + 1] + (1 if s[i] == '0' else 0)
    for i in range(0, n + 1):
      ans = min(ans, left[i] + right[i])
    return ans
class Solution {
  public int minFlipsMonoIncr(String s) {
    int n = s.length();
    int[] left = new int[n + 1];
    int[] right = new int[n + 1];
    int ans = Integer.MAX_VALUE;
    for (int i = 1; i <= n; i++) {
      left[i] = left[i - 1] + (s.charAt(i - 1) == '1' ? 1 : 0);
    }
    for (int i = n - 1; i >= 0; i--) {
      right[i] = right[i + 1] + (s.charAt(i) == '0' ? 1 : 0);
    }
    for (int i = 0; i <= n; i++) {
      ans = Math.min(ans, left[i] + right[i]);
    }
    return ans;
  }
}
class Solution {
public:
  int minFlipsMonoIncr(string s) {
    int n = s.size();
    vector<int> left(n + 1, 0), right(n + 1, 0);
    int ans = INT_MAX;
    for (int i = 1; i <= n; ++i) {
      left[i] = left[i - 1] + (s[i - 1] == '1');
    }
    for (int i = n - 1; i >= 0; --i) {
      right[i] = right[i + 1] + (s[i] == '0');
    }
    for (int i = 0; i <= n; i++) {
      ans = min(ans, left[i] + right[i]);
    }
    return ans;
  }
};
func minFlipsMonoIncr(s string) int {
  n := len(s)
  left, right := make([]int, n+1), make([]int, n+1)
  ans := math.MaxInt32
  for i := 1; i <= n; i++ {
    left[i] = left[i-1]
    if s[i-1] == '1' {
      left[i]++
    }
  }
  for i := n - 1; i >= 0; i-- {
    right[i] = right[i+1]
    if s[i] == '0' {
      right[i]++
    }
  }
  for i := 0; i <= n; i++ {
    ans = min(ans, left[i]+right[i])
  }
  return ans
}
/**
 * @param {string} s
 * @return {number}
 */
var minFlipsMonoIncr = function (s) {
  const n = s.length;
  let presum = new Array(n + 1).fill(0);
  for (let i = 0; i < n; ++i) {
    presum[i + 1] = presum[i] + (s[i] == '1');
  }
  let ans = presum[n];
  for (let i = 0; i < n; ++i) {
    ans = Math.min(ans, presum[i] + n - i - (presum[n] - presum[i]));
  }
  return ans;
};

Solution 2

class Solution:
  def minFlipsMonoIncr(self, s: str) -> int:
    n = len(s)
    presum = [0] * (n + 1)
    for i, c in enumerate(s):
      presum[i + 1] = presum[i] + int(c)
    ans = presum[-1]
    for i in range(n):
      ans = min(ans, presum[i] + n - i - (presum[-1] - presum[i]))
    return ans
class Solution {
  public int minFlipsMonoIncr(String s) {
    int n = s.length();
    int[] presum = new int[n + 1];
    for (int i = 0; i < n; ++i) {
      presum[i + 1] = presum[i] + (s.charAt(i) - '0');
    }
    int ans = presum[n];
    for (int i = 0; i < n; ++i) {
      ans = Math.min(ans, presum[i] + n - i - (presum[n] - presum[i]));
    }
    return ans;
  }
}
class Solution {
public:
  int minFlipsMonoIncr(string s) {
    int n = s.size();
    vector<int> presum(n + 1);
    for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + (s[i] == '1');
    int ans = presum[n];
    for (int i = 0; i < n; ++i) ans = min(ans, presum[i] + n - i - (presum[n] - presum[i]));
    return ans;
  }
};
func minFlipsMonoIncr(s string) int {
  n := len(s)
  presum := make([]int, n+1)
  for i, c := range s {
    presum[i+1] = presum[i] + int(c-'0')
  }
  ans := presum[n]
  for i := range s {
    ans = min(ans, presum[i]+n-i-(presum[n]-presum[i]))
  }
  return ans
}

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

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

发布评论

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