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.
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 ...
随机推荐
- js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)
// 验证开头不为零的正整数 WST.zhengZhengShuIn = function (className){ var rex = /^[1-9]{1}[0-9]*$/;//正整数 $(&quo ...
- Oracle 基础表查询
--查询所有用户表的数据 SELECT * FROM ALL_TABLES WHERE OWNER='USER_NAME' --如果是用该用户登录使用以下语句: SELECT * FROM USER_ ...
- Windows10电脑安装macOS Mojave系统的方法(最新版系统,含超详细步骤截图)
一.环境及准备工作 1.主机系统:本人系统是Windows10家庭中文版 2.虚拟机软件:VMware Workstation 14 Pro 虚拟机版本号:14.1.1 build-7528167 虚 ...
- UEdit百度富文本编辑器
1.下载地址:http://ueditor.baidu.com/website/download.html 2.引入js/css/语言包 3.表单id设置 3.js代码
- [Selenium]显式等待 Explicit wait & 隐式等待 Implicit wait
显式等待 Explicit wait 显示等待 , 就是明确的要等到某个元素出现或者某个元素满足某种条件,每隔一段时间检查一次,等不到,就一直等,如果在规定的时间内还没有找到,就跳出来检查间隔的时间和 ...
- extjs 学习一
环境 : eclipse ext tomcat 6 将下载的extjs 解压后全部 导入到项目中 .使用时 <!-- 1.引入样式 2.引入库文件 ,底层驱动 3. ext-all--&g ...
- 分组取前N记录
分组取前N记录 经常看到问题,如何取出每组的前N条记录.方便大家参考于是便把常见的几种解法列出于下. 问题:有表 如下,要求取出各班前两名(允许并列第二)Table1+----+------+-- ...
- 【Maven】Nexus配置和使用
Nexus安装 nexus安装,可以参照:[Maven]Nexus(Maven仓库私服)下载与安装 Nexus简单说明 用途:指定私服的中央地址.将自己的Maven项目指定到私服地址.从私服下载中央库 ...
- Java知识系列 -- 反射
原理 要想理解 Java 反射,首先要弄清类的加载过程. 比如这行代码 Person p = new Person();. 我们想要创建一个 Person 对象,并用 p 作为对象的引用. 在 Jav ...
- docker 命令介绍
查看镜像 docker images: 列出imagesdocker images -a :列出所有的images(包含历史)docker images --tree :显示镜像的所有层(layer) ...