-
Notifications
You must be signed in to change notification settings - Fork 4
/
flipZeroesMaximizeOnes.py
68 lines (51 loc) · 2.43 KB
/
flipZeroesMaximizeOnes.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Time complexity: O(n)
# Using sliding window strategy.
from typing import List
def flip_m_zeroes_largest_subarray_with_max_ones(arr, m):
"""
Algorithm -
- While `zeroes_count` is no more than `m` : expand the window to the right (w_right++) and increment the zeroes_count.
- While `zeroes_count` exceeds `m`, shrink the window from left (w_left++), decrement `zeroes_count`.
- Update the widest window(`best_w_left`, `best_w_size`) along the way. The positions of output 0's are inside the best window.
:param arr: Input array.
:param m: Maximum number of 0's that can be flipped in `arr` in order to get largest window/sub-array of 1's.
:return:
"""
w_left = w_right = best_w_left = best_w_size = zeroes_count = 0
while w_right < len(arr):
if zeroes_count <= m:
if arr[w_right] == 0:
zeroes_count += 1
w_right += 1
if zeroes_count > m:
if arr[w_left] == 0:
zeroes_count -= 1
w_left += 1
curr_w_size = w_right - w_left
if curr_w_size > best_w_size:
best_w_left = w_left
best_w_size = curr_w_size
best_w_right = best_w_left + best_w_size - 1
best_w = (best_w_left, best_w_right)
flip_zero_at_indices = []
for i in range(best_w_left, best_w_right+1): # for i=0; i < best_w_size; i++
if arr[i] == 0:
flip_zero_at_indices.append(i)
return {
"largestWindow": best_w,
"largestWindowSize": best_w_size,
"flipZeroAtIndices": flip_zero_at_indices
}
if __name__ == '__main__':
################### TC - 1 ###################
nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]
threshold = 2
window_details = flip_m_zeroes_largest_subarray_with_max_ones(nums, threshold)
print("Flip 0's at indices {flipZeroAtIndices} to get the maximum window/sub-array of size {largestWindowSize} " \
"where window start & end indices are {largestWindow}".format(**window_details))
################### TC - 2 ###################
nums = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1]
threshold = 3
window_details = flip_m_zeroes_largest_subarray_with_max_ones(nums, threshold)
print("Flip 0's at indices {flipZeroAtIndices} to get the maximum window/sub-array of size {largestWindowSize} " \
"where window start & end indices are {largestWindow}".format(**window_details))