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. JAVA规则引擎JSR-94笔札

    JAVA规则引擎JSR-94笔札 JSR-94 是由JCP(Java Community Process)组织所制定的java规则引擎API的java请求规范.它主要定义了规则引擎在java运行时的一 ...

  2. 【原创】有关Silverlight中异常“XmalParseEception” 通用解决思路

    针对于 此类 XamlParse问题,大部分都是 silverlight/WPF 前段 xmal文件问题,仔细逐行审查 接口解决.

  3. 使用jmeter工具测试上传接口

    1.方法选择post:上传都是post上传. 2.路径输入正确的上传接口路径,并勾选Use multipart/form-data for POST 3.添加文件,文件路径尽量不要有中文,防止编码问题 ...

  4. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...

  5. web项目传classes目录项目正常,打包成jar不能运行。

    笔者最近使用tomcat9,由于工作洁癖,盯上了tomcat启动日志里的"No TLD files were found in"字样,如下 15-Sep-2017 02:19:09 ...

  6. Python发送邮件不需要发件人密码认证

    #!/usr/bin/python # coding: UTF-8 import smtplib from email.mime.text import MIMEText receivers_list ...

  7. IPEndPoint

    .NET框架用IPEndPoint 对象来表示一个特定的IP地址和端口的组合,应用该对象的场景多是在讲socket绑定到本地地址或者将socket绑定到非本地地址.

  8. PhpStorm 2017 激活办法

    最新版PhpStorm 2017正式版改进了PHP 7支持,改进代码完成功能.PhpStorm 绝对是PHP开发最好的开发工具. phpstorm已经升级到2017.1,原注册码失效,2017.1.2 ...

  9. Cookis与文件缓存的区别

    我是一位顶尖的网络专家.稍后更新...

  10. java bulid path 和 WEB-INF/lib 下jar 包区别

    用Java Build Path导入包和把包复制到lib下是有区别的,它俩其实不会冲突,也没有什么关系的, Java Build Path是我们编译需要的包, 导入到lib下是程序运行时需要的包 ,  ...