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), 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 <= S.length <= 20000
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的更多相关文章
- [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), ...
- 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), ...
- 【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 ...
- 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...
- [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), ...
- 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), ...
- 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 ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
随机推荐
- shell 判断月末日期
有一个需求,根据输入的时间段,在这个时间段中的是月末的日期执行脚本 解决如下: #!/bin/bashif [ -z $1 ]thenecho "请输入年月日8位开始日期"exit ...
- 【坑】new一个对象时,tostring报空指针异常
错误环境 java1.8.0_111 错误描述 对某vo进行了修改,属性类型由long修改为了Long,getter.setter没有修改类型 调用该对象的new方法时抛出npe 错误原因 没深究.. ...
- Linux内核的arch目录
- CAFFE(一):Ubuntu 下安装CUDA(安装:NVIDIA-384+CUDA9.0+cuDNN7.1)
(安装:NVIDIA-384+CUDA9.0+cuDNN7.1) 显卡(GPU)驱动:NVIDIA-384 CUDA:CUDA9.0 cuDNN:cuDNN7.1 Ubuntu 下安装CUDA需要装N ...
- 一 :了解MVC
介绍 1. ASP.NET WebForm和ASP.NET MVC是并行的关系.都是属于.NET框架下的子框架. 2. MVC项目常用模板 空模板 : 不包含MVC目录结构,需要自己添加. 基本模 ...
- (初级篇)docker基础应用--01
命令 获取镜像: docker pull //: ,如:docker pull nginx 查看镜像列表: docker images 查看镜像信息: docker inspect
- qjson中把记录或类型或泛型数组转换为json字符串
unit Unit4; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 如何将html页面导出word格式?
近期做的项目也是奇葩,页面上需要导出excel,pdf,还有导出图片等...,刚把前几个怂好,还有下载成word文件,如何处理? function getOutword (id, fileName) ...
- python豆知识: for和while的else语句。
for语句,当可迭代对象耗尽后执行else语句. while循环,当条件为False后执行else. a = 1 while a != 10: a += 1 else: print(a)
- RAID 10是将RAID 1和RAID 0结合
RAID 10是将RAID 1和RAID 0结合,它的优点是同时拥有RAID 0的超凡速度和RAID 1的数据高可靠性,但是CPU占用率同样也更高,而且磁盘的利用率比较低.由于利用了RAID 0极高的 ...