【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), ...
随机推荐
- 神经网络学习笔记(二):feedforward和feedback
维基百科解释: Feed-forward, sometimes written feedforward, is a term describing an element or pathway with ...
- C++ 概率算法 利用蒙特卡罗算法计算圆周率
概率算法大致可分为4种形式: 数值概率算法: 蒙特卡罗算法: 拉斯维加斯算法: 舍伍德算法: 计算蒙特卡罗概率的算法实现: #include "stdio.h" #include ...
- 项目中有 xxxx 不能被json序列化
遇到这类问题 ,首先断点调试,看看要序列化的值 是一个什么类型的值 查看值得数据类型 在将值转化成可以被json序列化的对象 此时即可解决问题 如遇到 requests.post() 朝一个url发 ...
- spring-boot整合mybaits多数据源动态切换案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- 4412 Linux设备总线
总线_设备_驱动注册流程详解 注册流程图 • 设备一般都需要先注册,才能注册驱动– 现在越来越多的热拔插设备,反过来了.先注册驱动,设备来了再注册 设备 • 本节使用的命令– 查看总线的命令#ls / ...
- wangeditor 粘贴word内容带样式
这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...
- angualr项目引入容联 七陌7mroo
最近项目要求在注册页面增加客服服务浮窗,各种查找资料准备采用7moor来实现.现记录一下实现过程,便于后期查看: 引入7moor浮窗有两种方式: 1.h5方式,这种情况一般是单独打开新页面即可: 直接 ...
- js中Array方法重写(二):myForEach;myEvery;mySome;myFilter;myReduce
一.myForEach //myForeach 数组每个元素都执行一次回调函数 Array.prototype.myForEach = function(callback){ for(var i = ...
- python中*args和**kargs得用法总结
前言: 一个很简单的函数说起: def Jiafa(x, y): z = x + y return z print(Jiafa(1,2)) 这个很简单,一看就知道输出等于3. 那下一个问题是,如果我要 ...
- 请教怎么查询ORACLE的历史操作记录!
请问如何查询ORACLE的历史操作记录!!!!!我用的是linux oracle 11g r2,想查一下前几天的数据库的历史操作记录,例如对表的insert,delete,update等等的操作记录, ...