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. 【Git】一、安装、配置和仓库创建

    之前一直使用图形界面的git,只会一些最常用的操作,并没有说深入学习git的全部功能 开发这么久了,觉得是时候学习一下指令操作,更快捷也更bigger ------------------------ ...

  2. Hadoop_32_HDFS高可用机制

    1.高可靠概念 HA(High Available):高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动 节点及备用节点 2.Hadoop的HA运作机制: :正式引入 ...

  3. 用 Spark 处理复杂数据类型(Array、Map、JSON字符串等)

    split 对列表进行切割,然后生产新的列表 from pyspark.sql.functions import split# 对 key列按照 0-9之间的数字进行风格 重新命名为 s  df.se ...

  4. struts2之关联查询(5)

    需要关联四个表 select g.*,b.name brname,s.bid,s.sname,bi.name biname  from T_GOODS g left join T_brand b on ...

  5. springboot2.0入门(五)--swagger2接口API构建

    一.特点 代码变,文档变.只需要少量的注解,Swagger 就可以根据代码自动生成 API 文档,很好的保证了文档的时效性. 跨语言性,支持 40 多种语言. Swagger UI 呈现出来的是一份可 ...

  6. Luogu P2532 [AHOI2012]树屋阶梯 卡特兰数

    接着压位OvO... 我不会告诉你答案就是卡特兰数... 为什么呢? 首先,$ans[0]=1,ans[1]=1,ans[2]=2$ 对于$ans[3]$,我们可以发现他是这样来的: $ans[3]= ...

  7. How to Set Up Chinese Characters on Windows 7

    How to Change the Display Language of non-Unicode Programs in Windows http://www.7tutorials.com/chan ...

  8. MIME协议(五) -- MIME邮件的编码方式

    5  MIME邮件的编码方式 由于每个ASCII码字符只占用一个字节(8个bit位),且最高bit位总为0,即ASCII码字符中的有真正意义的信息只是后面的7个低bit位,而传统的SMTP协议又是基于 ...

  9. Cache As Ram

    1. Why need Cache? Cache又称之为高速缓存,是为了弥补CPU和主存之间逐渐增大的速度上的差距 而被迫在两者之间加入的一个小的SRAM.现代处理器上通常都会有三级或者是四级缓存,这 ...

  10. css background之设置图片为背景技巧

    原文 Background是什么意思,翻译过来有背景意思.同样在css里面作为css属性一成员同样是有背景意思,并且是设置背景图片.背景颜色.背景图片截取等样式. 首先先来看看background有那 ...