Skip to content

Commit 57b824a

Browse files
committed
Added longest ones algo (with generalization) to GrokkingSlidingWindow
1 parent 3299bec commit 57b824a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

GrokkingSlidingWindow.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cunningdj.grokJava;
22

3+
import java.util.Arrays;
34
import java.util.HashMap;
45
import java.util.HashSet;
56

@@ -41,6 +42,20 @@ public static void main(String[] args) {
4142
tester.intEquals(5, longestSubstringDistinctCharacters("abcde"), testTitle);
4243
// TESTING: only one
4344
tester.intEquals(1, longestSubstringDistinctCharacters("a"), testTitle);
45+
46+
// REPEATING_ONES_WITH_REPLACEMENT
47+
testTitle = "REPEATING_ONES_WITH_REPLACEMENT";
48+
// TESTING: k=1 (right/left/middle zero)
49+
tester.intEquals(4, repeatingOnesWithReplacement(new int[]{0,1,1,1}, 1), testTitle);
50+
tester.intEquals(4, repeatingOnesWithReplacement(new int[]{1,0,1,1}, 1), testTitle);
51+
tester.intEquals(4, repeatingOnesWithReplacement(new int[]{1,1,1,0}, 1), testTitle);
52+
// TESTING: k=0, all zeroes
53+
tester.intEquals(0, repeatingOnesWithReplacement(new int[]{0,0}, 0), testTitle);
54+
// TESTING: k=0, (right/left/middle zero)
55+
tester.intEquals(3, repeatingOnesWithReplacement(new int[]{0,1,1,1}, 0), testTitle);
56+
tester.intEquals(2, repeatingOnesWithReplacement(new int[]{1,0,1,1}, 0), testTitle);
57+
tester.intEquals(3, repeatingOnesWithReplacement(new int[]{1,1,1,0}, 0), testTitle);
58+
// tester.intEquals(00, repeatingOnesWithReplacement(new int[]{}, 00), testTitle);
4459
}
4560

4661
public static int longestSubstringDistinctCharacters(String str) {
@@ -122,4 +137,50 @@ public static double[] slidingAverage(int[] values, int windowSize) {
122137
}
123138
return averages;
124139
}
140+
141+
public static int repeatingOnesWithReplacement(int[] zeroes_ones, int k) {
142+
// Because I want to use the generalized solution to demonstrate how the logic generalizes,
143+
// I'll convert int[] to Integer[] (so it can match Object[]). This wouldn't be as efficient
144+
// as just non-generalizing this logic for int[], but we can extrapolate that solution
145+
// pretty easily from the logic in repeatingValidValuesLengthWithReplacement
146+
Integer[] converted = Arrays.stream(zeroes_ones).boxed().toArray(Integer[]::new);
147+
return repeatingValidValuesLengthWithReplacement(converted, 1, k);
148+
}
149+
150+
public static int repeatingValidValuesLengthWithReplacement(Object[] values, Object VALID_VAL, int k) {
151+
/*
152+
* Given an array of values and a valid value V and a number K, find the longest subarray
153+
* with only these valid values if you can replace K values
154+
*/
155+
// Space: O(1)
156+
int maxLength = 0;
157+
int start = 0;
158+
int invalidValsCount = 0;
159+
160+
// Time: O(n)
161+
for (int end=0; end < values.length; ++end) {
162+
if (values[end] != VALID_VAL) {
163+
invalidValsCount++;
164+
}
165+
if (invalidValsCount > k) {
166+
// Time: O(n-1) max *over the course of the algo. start can only increment upward and reach a max value of n-1
167+
while (start < end && values[start] == VALID_VAL) {
168+
start++;
169+
}
170+
// Time: O(1)
171+
if (start < end) {
172+
start++;
173+
invalidValsCount--;
174+
}
175+
}
176+
// Time: O(1)
177+
if (invalidValsCount <= k) {
178+
maxLength = Math.max(maxLength, end-start+1);
179+
}
180+
}
181+
182+
// Total Time: O(n) ( O(n) + O(n-1) + O(1) )
183+
// Total Space: O(1)
184+
return maxLength;
185+
}
125186
}

0 commit comments

Comments
 (0)