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 <= 20000Sonly 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 ...
随机推荐
- 什么时候用assert
assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制.在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证 ...
- RobHess的SIFT代码解析之RANSAC
平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码:SIFT+KD ...
- Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.
修改 my.ini 加上 max_allowed_packet =6710886467108864=64M默认大小4194304 也就是4M修改完成之后要重启mysql服务,如果通过命令行修改就不用 ...
- 6.Tray Monitor服务(监控服务)
1. Tray Monitor服务(监控服务) 该服务需要运行在gui环境下,用于查看baclua client.存储等状态.下面以windows下安装为例. 1.1. Tray Monito ...
- linux-2.6.38poll机制简析(以tiny6410按键中断程序为基础)
一.应用程序 /* struct pollfd { int fd; //文件描述符 short events; //表示请求检测的事件 short revents; //表示检测之后返回的事件 }; ...
- Python求均值,方差,标准差
import numpy as nparr = [1,2,3,4,5,6]#求均值arr_mean = np.mean(arr)#求方差arr_var = np.var(arr)#求标准差arr_st ...
- P4151 最大XOR和路径 线性基
题解见:https://www.luogu.org/problemnew/solution/P4151 其实就是找出所有环 把环上所有边异或起来得到的值扔到线性基里面 然后随便走一条从1~n的链 最后 ...
- 手摸手教你让Laravel开发Api更得心应手
https://www.guaosi.com/2019/02/26/laravel-api-initialization-preparation/ 1. 起因 随着前后端完全分离,PHP也基本告别了v ...
- Java队列与栈转换中String.Valueof()使用
1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...
- C# 委托、lambda表达式和事件 (7) 持续更新
引用方法 在C++,函数指针只不过是一个指向内存位置的指针,它不是类型安全的. C# 委托 定义了返回类型和参数的类型.委托类包含对方法的引用,还可以包含多个方法引用. 定义委托 public del ...