【leetcode】995. Minimum Number of K Consecutive Bit Flips
题目如下:
In an array
A
containing only 0s and 1s, aK
-bit flip consists of choosing a (contiguous) subarray of lengthK
and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.Return the minimum number of
K
-bit flips required so that there is no 0 in the array. If it is not possible, return-1
.Example 1:
Input: A = [0,1,0], K = 1
Output: 2
Explanation: Flip A[0], then flip A[2].Example 2:
Input: A = [1,1,0], K = 2
Output: -1
Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1].Example 3:
Input: A = [0,0,0,1,0,1,1,0], K = 3
Output: 3
Explanation:
Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0]
Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0]
Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]Note:
1 <= A.length <= 30000
1 <= K <= A.length
解题思路:如果A[0]是0,那么A[0]一定要翻转一次,当然翻转三次也是可以的,但是效果和翻转一次是一样的。接下来就可以忽略A[0]了,假设把A[0]删掉,那么A[1]就变成A[0],和之前对A[0]的操作是一样的,这也意味着可以从头遍历数组,遇到元素值为0就执行依次翻转操作。因为翻转A[i]意味着自身以及后面的K-1个元素都要翻转一次,所有可以把执行了翻转操作的下标存入列表flip_path,很显然flip_path的元素是升序排列的。最后从头开始遍历A,假设当前遍历到的元素为A[i],先从flip_path中找出前面执行的翻转操作有几个对自身有影响,有几个则翻转几次。如果翻转玩后恰好是1则继续遍历下一个元素,如果是0则判断i+K是否大于A的长度,大于表示无法翻转,返回-1,小于的话翻转次数加1,同时把i存入flip_path,继续下一个元素。
代码如下:
class Solution(object):
def minKBitFlips(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
import bisect
flip = []
res = 0
for i in range(len(A)):
if len(flip) > 0 and flip[0] + K <= i:
flip.pop(0)
inx = bisect.bisect_left(flip,i)
if inx % 2 == 1:
A[i] = 0 if A[i] == 1 else 1
if A[i] == 1:
continue
else :
if i + K > len(A):
return -1
A[i] = 1
flip.append(i)
res += 1
return res
【leetcode】995. Minimum Number of K Consecutive Bit Flips的更多相关文章
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [LeetCode] Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...
- [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...
- 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...
- 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【leetcode】452. Minimum Number of Arrows to Burst Balloons
题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
随机推荐
- Python的"random"函数的使用(一)
random.randrange(1,10) 随机产生0~7之间的整数,不包含7. random.sample(range(100), 5) 随机从range(100)中产生5个数,放入一个list. ...
- js-放大镜效果
jd或者淘宝的具体商品有个放大镜的效果.虽然网上类似插件琳琅满目,应用到项目上有诸多不便,自己抽点时间自己写了个类似插件,积累下代码,何乐而不为呢!!let‘go: 打算把此特效封装成个插件,先把最基 ...
- 回炉Spring--事务及Spring源码
声明式事务 配置文件信息: /** * @EnableTransactionManagement 开启基于注解的事务管理功能 * 1.配置数据源 * 2.配置事务管理器来管理事务 * 3.给方法上标注 ...
- Delphi fmx 找不到android设备解决办法
刚接触到移动开发,很多不熟悉.配置好Android SDK后,如果用模拟器来调试程序的话,那速度会让人崩溃,我用的Nexus7平板,插上电脑,开启USB调试,但奇怪在Delphi里就是找不到 ...
- 日志数据如何同步到MaxCompute
摘要:日常工作中,企业需要将通过ECS.容器.移动端.开源软件.网站服务.JS等接入的实时日志数据进行应用开发.包括对日志实时查询与分析.采集与消费.数据清洗与流计算.数据仓库对接等场景.本次分享主要 ...
- 2019 GNTC 阿里云参会分享:云原生SDWAN网络2.0 一站式上云服务
本次10/22-24 南京2019 GNTC大会上,阿里云网络云原生SDWAN网络2.0 由于独特的云原生定位.创新的解决方案,及成熟的应用案例.行业用户,获得行业媒体C114中国通信网.产业专家高度 ...
- BZOJ 2761: [JLOI2011]不重复数字 set
Description 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 ...
- Xcode cannot run using the selected device after upgrade
Please follow below step 1>Go to Project Build setting 2>Change compiler for c/c++/objective c ...
- SQL 关键字的使用顺序
1.查询中用到的关键词主要包含六个,并且他们的顺序依次为 select --> from --> where --> group by --> having --> or ...
- ALM11服务器IP变更相关配置修改
最近项目新增了网络控制,需要把ALM服务器迁移到新的区域.服务器整体复制后更改了IP地址. ALM与Oracle在同一台服务器(windows server 2008 R2) ALM的配置也需要做如下 ...