Welcome to Subscribe On Youtube

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

  • 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;
        }
    };
    
    
  • 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
    
    
  • 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;
    };
    
    
  • function minFlipsMonoIncr(s: string): number {
        let tot = 0;
        for (const c of s) {
            tot += c === '0' ? 1 : 0;
        }
        let [ans, cur] = [tot, 0];
        for (let i = 1; i <= s.length; ++i) {
            cur += s[i - 1] === '0' ? 1 : 0;
            ans = Math.min(ans, i - cur + tot - cur);
        }
        return ans;
    }
    
    

All Problems

All Solutions