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
Sof'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
Smonotone 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.
Approach #1: DP + Prefix + Suffix. [Java]
class Solution {
public int minFlipsMonoIncr(String S) {
int n = S.length();
int[] l = new int[n+1];
int[] r = new int[n+1];
l[0] = S.charAt(0) - '0';
r[n-1] = '1' - S.charAt(n-1);
for (int i = 1; i < n; ++i)
l[i] = l[i-1] + S.charAt(i) - '0';
for (int j = n-2; j >= 0; --j)
r[j] = r[j+1] + '1' - S.charAt(j);
int ret = r[0];
for (int i = 1; i <= n; ++i)
ret = Math.min(ret, l[i-1] + r[i]);
return ret;
}
}
Analysis:
l[i] = of min flips -> S[0] ~ S[i] all 0s.
r[i] = of min flips -> S[i] ~ S[n-1] all 1s.
ans = min{l[i-1] + r[i], l[n-1], r[0]}
l[i] = l[i-1] + s[i] == '1'
r[i] = r[i+1] + s[i] == '0'
Time complexity: O(n)
Space complexity: O(n)
Approach #2: DP. [C++]
class Solution {
public:
int minFlipsMonoIncr(string S) {
int n = S.length();
vector<vector<int>> dp(n+1, vector<int>(2, 0));
for (int i = 1; i <= n; ++i) {
if (S[i-1] == '0') {
dp[i][0] = dp[i-1][0];
dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1;
} else {
dp[i][0] = dp[i-1][0] + 1;
dp[i][1] = min(dp[i-1][0], dp[i-1][1]);
}
}
return min(dp[n][0], dp[n][1]);
}
};
Analysis:
dp[i][0] = ans of S[0, i-1] and S[i] == '0'.
d[i][1] = ans of S[0, i-1] and S[i] == '1'.
if S[i] == '0':
dp[i][0] = dp[i-1][0];
dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1;
else:
dp[i][0] = dp[i-1][0];
dp[i][1] = min(dp[i-1][0], dp[i-1][1])
Reference:
https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-926-flip-string-to-monotone-increasing/
926. Flip String to Monotone Increasing的更多相关文章
- 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), ...
- [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计算 动态规划 参考资料 日期 题目地址 ...
- [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), ...
- [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 ...
随机推荐
- linux引导系统
一.linux引导系统 1.选择操作系统 /etc/grub.conf 设置grub引导装载程序口令,使用单用户模式时必须输入此密码 password --md5 md5后的密码字符串(可以通过gru ...
- Thread(线程)和ThreadPool(线程池) Thread回调与返回值
Thread(线程) Thread开启线程:接收一个参数 TestClass tc = new TestClass(); //没有返回值,有一个object类型的参数的委托:两种写法. Paramet ...
- phpstorm2018.3的安装和激活和汉化
安装 第一步:解压并打开文件,运行安装程序,点击Next进入下一步, 第二步:选择软件安装目录,自定义选择安装根目录--> 注意!后面还需要找安装目录里的文件,所以记住安装到一个比较容易查看的目 ...
- PAT 1047 编程团体赛(代码)
1047 编程团体赛(20)(20 分) 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出 ...
- spring mvc 默认页面
只需要在servlet.xml页面中添加如下配置: <mvc:view-controller path="/" view-name="login"/> ...
- 移动文件流的读写指针---fseek
函数原型:int fseek(FILE *stream,long offset,int origin) stream:文件指针, offset:偏移量,正数表示正向偏移,负数表示负向偏移.origin ...
- Devexpress VCL Build v2014 vol 14.2.1 beta发布
已经快到2015 年了. 14.2.1 beta 才出来了. 还好,有一些新东西. 官网地址 VCL Gauge Control Designed to clearly convey informat ...
- spark 与 hbase-server 集成版本问题
今天在使用spark存储hbase的时候遇到异常Exception in thread "main" java.lang.NoSuchMethodError: io.netty.b ...
- SqlCommand和SqlDataAdapter的区别
SqlDataAdapter对象 一.特点介绍1.表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连接.2.在SqlDataAdapter和DataSet之 ...
- Codeforces777C Alyona and Spreadsheet 2017-05-04 17:46 103人阅读 评论(0) 收藏
C. Alyona and Spreadsheet time limit per test 1 second memory limit per test 256 megabytes input sta ...