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. 1 <= S.length <= 20000
  2. S only 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的更多相关文章

  1. 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), ...

  2. [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), ...

  3. 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), ...

  4. [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), ...

  5. 【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 ...

  6. 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...

  7. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  8. [Swift]LeetCode738. 单调递增的数字 | Monotone Increasing Digits

    Given a non-negative integer N, find the largest number that is less than or equal to Nwith monotone ...

  9. 738. Monotone Increasing Digits 单调递增的最接近数字

    [抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...

随机推荐

  1. .htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration

    php项目 .htaccess文件配置如下: #文件缓存时间配置 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css)$"& ...

  2. 71.纯 CSS 创作一个跳 8 字型舞的 loader

    原文地址:https://segmentfault.com/a/1190000015534639#articleHeader0 感想:rotateX() 和rotateZ()一起使用好懵呀. HTML ...

  3. MySQL(索引)

    索引 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可. MySQL中常见索引有: 普通索引 唯一索引 ...

  4. python远程连接windows

    远程连接windows系统     https://blog.51cto.com/ckl893/2145809 import winrm win2008 = winrm.Session('http:/ ...

  5. tomcat启动成功后访问却404

    1.检查是否把项目添加进tomcat,好久不用tomcat这次就犯了这种低级错误 2.检查路径,tomcat中的访问路径与项目中设置的路径是否一样,因为这次有些配置文件直接复制的源码,但源码中项目名称 ...

  6. DLL 函数中使用结构体指针作函数参数(C# 调用 C++ 的 DLL)

    存在的问题: 问题1:C++ 与 C# 同样定义的结构体在内存布局上有时并不一致: 问题2:C# 中引入了垃圾自动回收机制,其垃圾回收器可能会重新定位指针所指向的结构体变量. 解决方案: 问题1方案: ...

  7. MM-分割评估

    SAP MM分割评估 https://blog.csdn.net/kangliujie/article/details/76681333 SAP MM批次管理分割评估 https://blog.csd ...

  8. configure,make,make install作用和关系的一些理解

    一. 整体关系 为求直观,画了一张大致关系图: 我个人的理解是这样的,将编译安装比作做菜的话, △ configure的作用,以厨师的构想以参数的形式作为输入,生成并输出菜谱,菜谱包含两个部分---- ...

  9. 2017-11-11 Sa Oct Is it online

    2017-11-11 Sa Oct Is it online 9:07 AM After breakfast I tried connecting to the course selection sy ...

  10. tornado 基于MongoDB存储 session组件开发

    1.开发伊始 根据源码中RequestHandler类中发现__init__函数中会调用自身initialize函数,此函数中为pass,即可以围绕initialize开发一系列的组件 2.开发实现 ...