【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), 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.
解题思路:转换后的字符串有两种形式,一个是全为0,另一个是前半部分全是0后半部分全是1。第一种情况的翻转次数是S中1的个数;第二种的情况,我们只要找出转换后的字符串第一个1所在的位置即可。设第一个所在的位置是i,那么S[0:i-1]区间内所有1都要变成0,而S[i:-1]区间内所有的0要变成1,总的翻转次数就是前一段区间内1的个数加上后一段区间内0的个数即可。遍历S,即可求出第二种情况的最小值,再与第一种情况求得的值比较取最小值即可。
代码如下:
class Solution(object):
def minFlipsMonoIncr(self, S):
"""
:type S: str
:rtype: int
"""
t_count_1 = S.count('')
t_count_0 = len(S) - t_count_1
res = t_count_1 count_0 = 0
count_1 = 0
for i,v in enumerate(S):
if v == '':
count_0 += 1
else:
#count_1 += 1
# if convert this 1 to 0
res = min(res,count_1 + (t_count_0 - count_0))
count_1 += 1
return res
【leetcode】926.Flip String to Monotone Increasing的更多相关文章
- 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...
- 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), ...
- 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】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- [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), ...
随机推荐
- Appium环境搭建(Appium库的安装)
Appium环境搭建 谷歌驱动和对照:注意:64位向下兼容,直接下载32位的就可以啦,亲测可用. https://blog.csdn.net/allthewayforward/article/deta ...
- VM安装OSX进度条一半时卡住不动,【附】OSX10.10 ISO镜像文件
安装OSX10.10真是一波多折,先是下载了一个5G多的原版dmg文件,转成ISO后在虚拟机上无法识别,后按网上的说的方法在提取出来的BaseSystem.dmg文件,再转成ISO文件,可以 ...
- 透明的UITableView
// // ViewController.m // 透明table // // Created by LiuWei on 2018/4/23. // Copyright © 2018年 xxx. Al ...
- JavaScript中的编码解码
1.URI 统一资源标识符(URI)是一个用于标识某一互联网资源名称的字符串.,该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作.Web上可用的每种资源 -HTML文档.图像 ...
- SPOJ - FTOUR2 (点分治+树状数组)
题目:https://vjudge.net/contest/307753#problem/I 题意:有一颗树,上面有白色黑色点,每个点上有一个权值,权值可以为负,现在我要求一条路径,权值和最大,这条路 ...
- 从xxxx检测到有潜在危险的 Request.Form 提示黄页
相信很多朋友都遇到过"从客户端xxxxxx"检测到有潜在危险的 Request.Form 然后给一个黄页提示.然后检测代码又找不到错误的代码提示. 原因:是因为在页面里边使用了富文 ...
- 高并发大流量专题---10、MySQL数据库层的优化
高并发大流量专题---10.MySQL数据库层的优化 一.总结 一句话总结: mysql先考虑做分布式缓存,过了缓存后就做mysql数据库层面的优化 1.mysql数据库层的优化的前面一层是什么? 数 ...
- HDU4762 Cut the Cake
HDU4762 Cut the Cake 思路:公式:n/m(n-1) //package acm; import java.awt.Container; import java.awt.geom.A ...
- drf基础
1.什么是编程? 数据结构和算法的结合 2.什么是REST? 同一个功能会产生五花八门的url(把查看单条记录和查看多条记录都看成是一个功能),而且响应回去的数据也没有同一的格式规范,这就造成了前后端 ...
- 关于日历实现代码里lunarInfo(农历)数组
var lunarInfo=new Array( 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x0 ...