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 ...
随机推荐
- mysql管理工具之pt
之前我一直用Seconds_behind_master来衡量主从的延迟,今天看到文档,才觉得多么不可靠!以下是官方文档的描述: In essence, this field measures the ...
- XGBOOST in WIN64 安装
参考:https://blog.csdn.net/zyghs/article/details/50897716 环境 platform:windows 10,64位 Python: Python3.7 ...
- IEAD工具教你创建maven项目
之前一直用的是其他的开发工具,maven到目前为止也就用了3个月,今天又时间整理一些初期的使用方法,仅供参照. 为什么要用maven 原因很简单,因为使用maven,会使得项目非常容易管理. 举个例子 ...
- 使用FindCmdLineSwitch处理命令行参数
一.四个形式(变体) .function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet; IgnoreCase: B ...
- mORMot学习笔记2-2种方式查询数据
本例使用SqlServer 第一种方式结果放入Memo控件,,需要引用SynCommons, SynDB, SynOleDb; procedure TForm1.Button1Click(Sender ...
- 第五章、前端之JQuery
目录 第五章.前端之JQuery 一.选择器 二.基本筛选器 三.样式操作 四.位置操作 五.文本操作 六.属性操作 七.文档处理 八.事件 九.动画效果 十.补充 第五章.前端之JQuery 一.选 ...
- Image Processing and Analysis_8_Edge Detection:Finding Edges and Lines in Images by Canny——1983
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- OpenCV入门学习资料汇总
OpenCV学习文档资料 OpenCV学习:1)OpenCV中文网站——http://wiki.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5 2)python实 ...
- 翻译应用将在Win8.1系统中取消下载安装
自Windows8.Windows Phone 7.1和Windows Phone 8受到影响之后,微软又正式宣布停止对翻译应用提供支持服务.Microsoft Translator这款应用将从Win ...
- spark streaming消费kafka: Java .lang.IllegalStateException: No current assignment for partition
1 原因是: 多个相同的Spark Streaming同时消费同一个topic,导致的offset问题.关掉多余的任务,就ok了.