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

We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'.

Return the minimum number of flips to make S monotone increasing.

Example 1:

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

Example 2:

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

Example 3:

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

Note:

  1. 1 <= S.length <= 20000
  2. S only consists of '0' and '1' characters.

Approach #1: DP + Prefix + Suffix. [Java]

class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int[] l = new int[n+1];
int[] r = new int[n+1];
l[0] = S.charAt(0) - '0';
r[n-1] = '1' - S.charAt(n-1);
for (int i = 1; i < n; ++i)
l[i] = l[i-1] + S.charAt(i) - '0';
for (int j = n-2; j >= 0; --j)
r[j] = r[j+1] + '1' - S.charAt(j);
int ret = r[0];
for (int i = 1; i <= n; ++i)
ret = Math.min(ret, l[i-1] + r[i]);
return ret;
}
}

Analysis:

l[i] = of min flips -> S[0] ~ S[i] all 0s.

r[i] = of min flips -> S[i] ~ S[n-1] all 1s.

ans = min{l[i-1] + r[i], l[n-1], r[0]}

l[i] = l[i-1] + s[i] == '1'

r[i] = r[i+1] + s[i] == '0'

Time complexity: O(n)

Space complexity: O(n)

  

Approach #2: DP. [C++]

class Solution {
public:
int minFlipsMonoIncr(string S) {
int n = S.length();
vector<vector<int>> dp(n+1, vector<int>(2, 0));
for (int i = 1; i <= n; ++i) {
if (S[i-1] == '0') {
dp[i][0] = dp[i-1][0];
dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1;
} else {
dp[i][0] = dp[i-1][0] + 1;
dp[i][1] = min(dp[i-1][0], dp[i-1][1]);
}
}
return min(dp[n][0], dp[n][1]);
}
};

  

Analysis:

dp[i][0] = ans of S[0, i-1] and S[i] == '0'.

d[i][1] = ans of S[0, i-1] and S[i] == '1'.

if S[i] == '0':

  dp[i][0] = dp[i-1][0];

  dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1;

else:

  dp[i][0] = dp[i-1][0];

  dp[i][1] = min(dp[i-1][0], dp[i-1][1])

Reference:

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/

926. Flip String to Monotone Increasing的更多相关文章

  1. LC 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  2. [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  3. 【leetcode】926.Flip String to Monotone Increasing

    题目如下: A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possib ...

  4. 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...

  5. [Swift]LeetCode926. 将字符串翻转到单调递增 | Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  6. Flip String to Monotone Increasing LT926

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  7. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  8. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

  9. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

随机推荐

  1. linux下每次git clone不需输入账号密码的方法

    在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: ame@zhenyun ~ $touch .git-credentials ame@zhenyu ...

  2. Golang之Mysql事务

    Mysql事务 )原子性 )一致性 )隔离性 )持久性 示例代码 package main import ( "fmt" _ "github.com/go-sql-dri ...

  3. [SoapUI] 设置HTTP Request的Header

    import com.eviware.soapui.support.types.StringToStringMap def hearderMap = new StringToStringMap() d ...

  4. JDK和Eclipse的下载路径

    JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse http://www.eclipse.or ...

  5. python的介绍和及基本的使用

    一 什么是计算机 1 计算机就是由一堆硬件组成的一个机器. 2 硬件的分类: CPU:犹如人类的大脑,运行着需要运行的程序. 内存:将 CPU要运行的内容从硬盘中读取出来,然后CPU在内存里拿内容,只 ...

  6. 2018.09.11 poj1845Sumdiv(质因数分解+二分求数列和)

    传送门 显然需要先求出ab" role="presentation" style="position: relative;">abab的所有质因 ...

  7. 前端程序员经常忽视的一个 JavaScript 面试题

    题目 function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function () { ...

  8. struts2 一些注解

    实现的JSP页面位置 web-root/jsp/user/add.jsp /update.jsp // /* @Namespace("/t") @AllowedMethods(va ...

  9. Android类装载器DexClassLoader的简单使用-----制作android插件的前奏

    声明:此篇文章借鉴<android内核剖析>整理得来. 一.装载器简介 “类装载器”(ClassLoader),顾名思义,就是用来动态装载class文件的.标准的Java SDK中有个Cl ...

  10. Spring 配置 事务的几种方式

    Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...