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. 基于Jenkins的持续集成CI

    CI(continuous integration)持续集成 一次构建:可能包含编译,测试,审查和部署,以及其他一些事情,一次构建就是将源代码放在一起,并验证软件是否可以作为一个一致的单元运行的过程. ...

  2. SSH三大框架需要的配置文件

    1. Struts2框架 * 在web.xml中配置核心的过滤器 <filter> <filter-name>struts2</filter-name> <f ...

  3. 菜刀连接webshell

    中国菜刀,一个非常好用而又强大的webshell,它可不是用来切菜的做饭的道具哦,是一款专业的网站管理软件,大小只有300多KB,真是小巧实用啊!不过被不法分子利用到,就是一个黑站的利器了.我记得以前 ...

  4. SqlServer中批量update

    现在我有两张表分别是S_PERSON,S_USER S_PERSON S_USER 我现在想把S_USER表中的ACCOUNT批量修改成S_PERSON的ACCOUNT 我们可以发现S_USER表中有 ...

  5. requestFullscreen()事件全屏不好使怎么解决

    标明:我在360和火狐中全屏requestFullscreen()事件不好使: 解释:我后来发现我的页面是在iframe框架中使用的并且没有设置allowfullscreen="true&q ...

  6. vue-awesome-swiper插件

    http://www.cnblogs.com/songrimin/p/6905136.html 这个地址不错 在上一些我的demo代码 第一步安装 npm install vue-awesome-sw ...

  7. C#设计模式之简单工厂模式(过渡模式)

    一.引言 之所以写这个系列,是了为了自己更好的理解设计模式,也为新手提供一些帮助,我都是用最简单的.最生活化的实例来说明.在上一篇文章中讲解了单例模式,今天就给大家讲一个比较简单的模式——简单工厂模式 ...

  8. linux下安装php php-fpm(转载)

    centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...

  9. CSS 关键的基础知识

    今晚看了 百度传课 一门关于CSS的课程, 感觉不错, 随手记了点儿笔记, 供以后查阅. =================================================== pos ...

  10. 2018.08.16 POJ1183反正切函数的应用(简单数学)

    传送门 代数变形一波. 显然有b,c>a. 那么这样的话可以令b=a+m,c=a+n. 又有a=(bc-1)/(b+c). 带入展开可知m*n=a*a+1. 要让m+n最小只需让m最大,这个结论 ...