- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path995.py
27 lines (24 loc) · 949 Bytes
/
995.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K 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.
Input: A = [0,1,0], K = 1
Output: 2
Explanation: Flip A[0], then flip A[2]
'''
classSolution:
defminKBitFlips(self, a: 'List[int]', k: 'int') ->'int':
fromcollectionsimportdeque
q=deque()
res=0
foriinrange(len(a)):
iflen(q) %2!=0:
ifa[i] ==1:
res+=1
q.append(i+k-1)
else:
ifa[i] ==0:
res+=1
q.append(i+k-1)
ifqandq[0] ==i: q.popleft()
ifqandq[-1] >=len(a): return-1
returnres