Skip to content

Commit 944549c

Browse files
committed
day31 - sliding window-longest-subarray w/ ones
1 parent 2706267 commit 944549c

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Daily Dose of Code
22

3+
- Day 32
4+
- #practice problem. [longest-substr-with-ones-after-replacement](practice/longest-subarray-with-ones-after-replacement.py)
35
- Day 31
46
- Deployed a [multiapp cluster](codegarage/kubernetes/multiapp-cluster-with-ingress/deployments/) in minikube. Created multi service with an ingress controller.
57
- Day 30
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
def length_of_longest_substring(arr, k):
2+
"""
3+
Given an array containing 0s and 1s, if you are allowed to replace no more than 'k'
4+
0s with 1s, find the length of the longest contiguous subarray having all 1s.
5+
6+
Example 1:
7+
8+
Input: Array=[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], k=2
9+
Output: 6
10+
Explanation: Replace the '0' at index 5 and 8 to have the longest contiguous subarray of 1s having length 6.
11+
Example 2:
12+
13+
Input: Array=[0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], k=3
14+
Output: 9
15+
Explanation: Replace the '0' at index 6, 9, and 10 to have the longest contiguous subarray of 1s having length 9.
16+
"""
17+
18+
# replace 0 with 1, up to k times
19+
# find the length of the substring with contiguous 1s
20+
# [0, 1, 0, 0, 1, 1, 0, 0, 0], k = 2. if we replace 0s at index 2 and 3,
21+
# length of the substring(1, 5) will be 5.
22+
23+
# for each substring.. we can do k replacements. the replacement can
24+
# be anywhere.. we have to try all positions. in substring [0, 1, 0, 0, 0, 1]
25+
# we can replace [1, 1, 1, 0, 0, 1] or [0, 1, 1, 1, 0, 1]
26+
27+
# brute forcing with each substring.
28+
# will be O(N^2)
29+
30+
len_of_longest_ones = 0
31+
32+
N = len(arr)
33+
for i in range(N):
34+
replacements = 0
35+
for j in range(i, N):
36+
if arr[j] == 0 and replacements >= k:
37+
# quit this substring if we crossed the replacement limit
38+
break
39+
40+
# XXX This is flawed. the replacement can be done further
41+
# down the substring too. this solution would fail.
42+
if arr[j] == 0:
43+
replacements += 1
44+
45+
len_of_longest_ones = max(len_of_longest_ones, j - i + 1)
46+
47+
return len_of_longest_ones
48+
49+
def length_of_longest_substring_v1(arr, k):
50+
# using a sliding window method?
51+
# window tracks the start and end of substring with 1s
52+
# extend the window if arr[i] == 1 or arr[i] == 0 and replacements < k
53+
54+
# some corner cases, arr with all ones or 0s.
55+
56+
N = len(arr)
57+
window_start = 0
58+
len_of_longest_ones = 0
59+
num_ones_in_substr = 0
60+
61+
for window_end in range(N):
62+
if arr[window_end] == 1:
63+
# extend the window without replacement
64+
num_ones_in_substr += 1
65+
66+
window_length = window_end - window_start + 1
67+
if (window_length - num_ones_in_substr) > k:
68+
# we've exceeded the replacements. time to shrink the window
69+
if arr[window_start] == 1:
70+
num_ones_in_substr -= 1
71+
window_start += 1
72+
window_length = window_end - window_start + 1
73+
74+
len_of_longest_ones = max(len_of_longest_ones, window_length)
75+
76+
return len_of_longest_ones
77+
78+
def test_length_of_longest_substring():
79+
testcases = [
80+
[
81+
[[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2], 6
82+
],
83+
[
84+
[[0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3], 9
85+
],
86+
[
87+
[[0, 1, 1, 0, 0], 2], 4
88+
],
89+
[
90+
[[0, 0, 0, 0, 0], 3], 3
91+
],
92+
[
93+
[[0, 0, 0, 0, 0], 0], 0
94+
],
95+
[
96+
[[1, 1, 1, 1, 1], 3], 5
97+
]
98+
]
99+
100+
for testcase in testcases:
101+
arr, k = testcase[0]
102+
expected_output = testcase[1]
103+
104+
actual_output = length_of_longest_substring_v1(arr, k)
105+
assert expected_output == actual_output, f'actual: {actual_output}, expected: {expected_output}, inp: arr={arr}, k={k}'
106+
107+
test_length_of_longest_substring()

0 commit comments

Comments
 (0)