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.

Runtime: 19 ms, faster than 17.65% of Java online submissions for Flip String to Monotone Increasing.

package date20190116;

public class flipstringtomonoincreasing926 {

  public static int minFlipsMonoIncr(String S) {
int[][] lefttoright = new int[S.length()][];
lefttoright[][] = S.charAt() == '' ? : ;
for(int i=; i<S.length(); i++){
lefttoright[i][] = (S.charAt(i) == '' ? : ) + lefttoright[i-][];
}
lefttoright[S.length()-][] = S.charAt(S.length()-) == '' ? : ;
for(int i=S.length()-; i>=; i--){
lefttoright[i][] = (S.charAt(i) == '' ? : ) + lefttoright[i+][];
}
// for(int[] x : lefttoright){
// System.out.print(x[0] + " ");
// }
int ret = S.length();
for(int i=; i<S.length()-; i++){
ret = Math.min(ret, lefttoright[i][]+lefttoright[i+][]);
}
ret = Math.min(ret, lefttoright[][]);
ret = Math.min(ret, lefttoright[S.length()-][]);
return ret;
}
public static void main(String[] args){
String s = "";
minFlipsMonoIncr(s); }
}

another solution

Runtime: 12 ms, faster than 68.98% of Java online submissions for Flip String to Monotone Increasing.

class Solution {
public int minFlipsMonoIncr(String S) {
int[] dp = new int[S.length()+];
int N = S.length();
for(int i=; i < N; i++){
dp[i+] = dp[i] + (S.charAt(i) == '' ? : );
}
int ans = Integer.MAX_VALUE;
for(int i=; i<N+; i++){
ans = Math.min(ans, dp[i] + N-i - (dp[N] - dp[i]));
}
return ans;
} }

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

  1. [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), ...

  2. 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. LC 738. Monotone Increasing Digits

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

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

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

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

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

随机推荐

  1. 【fiddler】Fiddller的应用

    一.fiddler抓取移动端接口 1.获取PC端IP 2.手机ip设置为与电脑同一局域网ip并配置代理 1)手机ip地址与pc地址连接同一局域网网络 2)代理设置为手动,主机名为PCip,端口号为88 ...

  2. Linux gdb调试及后台程序问题

    https://blog.csdn.net/lengchanguo/article/details/50481533 转? 问题是后台& 调试

  3. JVM命令jinfo

          jinfo也是jvm中参与的一个命令,可以查看运行中jvm的全部参数,还可以设置部分参数.   格式      jinfo [ option ] pid      jinfo [ opti ...

  4. 按照教程自动安装RFNoC时.在使用pip安装pybombs时出现报错,解决办法

    $ sudo apt-get install git $ sudo apt-get install python-setuptools python-dev python-pip build-esse ...

  5. import this: python原则

    >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is ...

  6. Error creating bean with name 'documentationPluginsBootstrapper' defined in URL

    启动报错 Error starting ApplicationContext. To display the auto-configuration report re-run your applica ...

  7. InheritableThreadLocal——父线程传递本地变量到子线程的解决方式及分析

    转自https://blog.csdn.net/hewenbo111/article/details/80487252 上一个博客提到ThreadLocal变量的基本使用方式,可以看出ThreadLo ...

  8. MyBatis执行原理图

    作者:W&L 推荐: 陶邦仁的博客 (1)加载配置并初始化       触发条件:加载配置文件 配置来源于两个地方,一处是配置文件,一处是Java代码的注解,将SQL的配置信息加载成为一个个M ...

  9. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

  10. MFC 画字体DrawText()或TextOut(),CFont字体样式类

    新建单个文档的MFC应用程序,类视图——View项的属性——消息,WM_PAINT,创建OnPaint()函数 void CMFCApplication27View::OnPaint() { CPai ...