【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
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.
解题思路:转换后的字符串有两种形式,一个是全为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), ...
随机推荐
- Docker安装RMQ
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11752934.html 进入rabbitmq的docker hub镜像仓库地址:https://hub ...
- Joyoshare HEIC Converter for Mac将HEIC照片转换成其他格式的方法
如何把HEIC格式的照片转换成其JPEG,PNG,GIF他格式呢?使用Joyoshare HEIC Converter for Mac破解版就可以,Joyoshare HEIC Converter是可 ...
- sublime text 3插件下载教程
Sublime官网下载地址:http://www.sublimetext.com/ 安装插件:(插件包管理Preferences Browse Packages) 插件官网:http://www.fe ...
- Ext js-01 -helloworld
一.下载ext: 登陆这个网址 https://www.sencha.com/products/evaluate/ 下载下来解压后如下:安装cmd程序 二.开始helloworld 新建一个idea ...
- Spring学习总结(2)- AOP
一,什么是AOP AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中 ...
- php str_repeat()函数 语法
php str_repeat()函数 语法 str_repeat()函数怎么用? php str_repeat()函数用于重复使用指定字符串,语法是str_repeat(string,repeat), ...
- Cent OS (三)vi文本编辑操作
序号 命令 命令含义 1 echo 2 vi/vim 编辑 3 cat cat 命令用于连接文件并打印到标准输出设备上. 4 more 分屏显示文本内容 5 l ...
- 0.OpenCV框架
reference: https://docs.opencv.org/4.1.2/ 基本使用 1.图片和视频,读写(2,8) 2.OpenCV基本数据类型(3) 3.OpenCV大型数据类型及操作:图 ...
- laravel 中url使用
url() 通过url辅助函数(路由)生成:location.href = "{{url('user/index')}}"; 或者:location.href = "{{ ...
- python赞乎--学习开发