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), 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.
Idea 1. 由结果推算,if monotonic increasing string is composed of x zeros and (n-x) ones, based on the number of ones on the left and right side of str[x], the number of flips can be calculated as ones[x] + (n-x - (ones[n] - ones[x])), another example to use prefix sum to caculate ones.
flip from '1' -> '0' on the left: ones[x]
flip from '0' -> '1' on the right: n - x - (ones[n] - ones[x]) or scan the array from right to left
仔细corner case, 全部都是'0' or '1' monotonic increasing string.
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int[] ones = new int[n+1];
for(int i = 1; i <=n; ++i) {
ones[i] = ones[i-1] + S.charAt(i-1) - '0';
}
int result = Integer.MAX_VALUE;
for(int i = 0; i <= n; ++i) {
result = Math.min(result, ones[i] + (n - i) - (ones[n] - ones[i]));
}
return result;
}
}
Idea 1.b No need to build ones array, the number of ones can be computed while looping the array, just need the total number of ones in advance
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int totalOnes = 0;
for(int i = 0; i < S.length(); ++i) {
totalOnes += S.charAt(i) - '0';
}
int ones = 0;
int result = Integer.MAX_VALUE;
for(int i = 0; i <= n; ++i) {
if(i >= 1) {
ones += S.charAt(i-1) - '0';
}
result = Math.min(result, ones + (n - i) - (totalOnes - ones));
}
return result;
}
}
稍微简洁一点,把全身1的情况做初始值
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int totalOnes = 0;
for(int i = 0; i < S.length(); ++i) {
totalOnes += S.charAt(i) - '0';
}
int ones = 0;
int result = n - totalOnes;
for(int i = 1; i <= n; ++i) {
ones += S.charAt(i-1) - '0';
result = Math.min(result, ones + (n - i) - (totalOnes - ones));
}
return result;
}
}
Idea 2. Dynamic programming, 网上看到的更赞的方法, let dp[i-1] be the minimum number of flips to make S.substring(0, i) is monotonic increasing, how to extend the solution for S.charAt(i)?
dp[i] = dp[i-1] if S.charAt(i) == '1', nothing needed, as it still satisfy monotonic increasing string.
dp[i] = Math.min(ones[i-1], dp[i-1] + 1), if S.chart(i) == '0' either flip all the previous ones to 0; or flip the current '0' to '1' since S.substring(0, i) is monotonice, add '1' still satisfies the conidtion.
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int[] dp = new int[n+1];
int ones = 0;
for(int i = 1; i <= n; ++i) {
if(S.charAt(i-1) == '1') {
dp[i] = dp[i-1];
++ones;
}
else {
dp[i] = Math.min(dp[i-1] + 1, ones);
}
}
return dp[n];
}
}
Idea 2.b the above formula shows the current dp depends only on the previous number, the array dp[] is not needed
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int dp = 0;
int ones = 0;
for(int i = 1; i <= n; ++i) {
if(S.charAt(i-1) == '1') {
++ones;
}
else {
dp = Math.min(dp + 1, ones);
}
}
return dp;
}
}
Flip String to Monotone Increasing LT926的更多相关文章
- 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), ...
- [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), ...
- 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 (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计算 动态规划 参考资料 日期 题目地址 ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
随机推荐
- Jprofiler监控远程jvm
2.Windows打开安装好的jprofiler 1 3.配置步骤: vi catalina.sh 添加复制的配置保存退出后重新停启Tomcatsh shutdown.sh #停ps -ef|grep ...
- Tomcat(免安装版)的安装与配置【转】
1.下载 到Apache的官方网站,我们可以很容易找到Tomcat的下载地址,如: http://tomcat.apache.org/download-60.cgi 在这里我们可以下载到Tomcat的 ...
- OS模块学习
import os,sys s = os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 # print(s) # os.chdir('..') #改变当前工作目录 # s ...
- Url的拦截问题
如果 在中没有, 就会去找欢迎页,先找/index,jsp,在webapp里并没有,就继续往下找/index.html, 是图中的拦截形式,就会被servlet拦截,对应于controller中的请求 ...
- SQL注入--SQLMap过WAF
单引号被过滤情况: 空格.等号未被过滤情况: select被过滤情况: 以此类推,当sqlmap注入出现问题时,比如不出数据,就要检查对应的关键词是否被过滤. 比如空格被过滤可以使用space2com ...
- 41_redux_counter应用_react-redux版本
问题: redux与react组件的代码耦合度太高 编码不够简洁 react-redux 1)是一个react插件库 下载: npm install --save react-redux@5.0.6 ...
- The Ultimate Productivity Hack is Saying No
The Ultimate Productivity Hack is Saying No By James ClearRead this on JamesClear.com The ultimate p ...
- anujs1.4.0发布
经过三个月的埋头苦干,终于完成Fiber版的anujs. 主要特性有: 测试全部改成jest, 迁移官方测试用例.有许多迷你React吹得怎么天花乱坠,但是生命周期钩子的执行顺序无法与官方保持一致,那 ...
- 颜色空间之CIE2000色差公式
CIEDE2000色差公式 为了进一步改善工业色差评价的视觉一致性,CIE专门成立了工业色差评价的色相和明度相关修正技术委员会TC1-47(Hue and Lightness Dependent ...
- 一千行 MySQL 详细学习笔记
Windows服务 -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注意:等号与值 ...