-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path995.py
47 lines (47 loc) · 1.56 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
__________________________________________________________________________________________________
sample 884 ms submission
class Solution:
def minKBitFlips(self, a: List[int], k: int) -> int:
n = len(a)
first_zero = -1
looking_for = 0
index = -1
switch = -1
count = 0
switches = [0]*(n+1)
prev_s = 0
for i in range(n):
if 1==switches[i]:
looking_for = 1-looking_for
#print(count)
#print("i is "+str(i)+'i am looking for '+str(looking_for))
if a[i]==looking_for:
if n-i<k:
return -1
else:
looking_for = 1-looking_for
switches[i+k] = 1
count+=1
return count
__________________________________________________________________________________________________
sample 13824 kb submission
from collections import deque
class Solution:
def minKBitFlips(self, A: List[int], K: int) -> int:
flipped = deque()
curr = 0
ans = 0
for i, x in enumerate(A):
if i >= K:
curr ^= flipped[0]
if curr^A[i] != 1:
if len(A) - i < K: return -1
ans += 1
curr ^= 1
flipped.append(1)
else:
flipped.append(0)
if len(flipped) > K:
flipped.popleft()
return ans
__________________________________________________________________________________________________