|
1 | 1 | package com.cunningdj.grokJava; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
3 | 4 | import java.util.HashMap; |
4 | 5 | import java.util.HashSet; |
5 | 6 |
|
@@ -41,6 +42,20 @@ public static void main(String[] args) { |
41 | 42 | tester.intEquals(5, longestSubstringDistinctCharacters("abcde"), testTitle); |
42 | 43 | // TESTING: only one |
43 | 44 | 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); |
44 | 59 | } |
45 | 60 |
|
46 | 61 | public static int longestSubstringDistinctCharacters(String str) { |
@@ -122,4 +137,50 @@ public static double[] slidingAverage(int[] values, int windowSize) { |
122 | 137 | } |
123 | 138 | return averages; |
124 | 139 | } |
| 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 | + } |
125 | 186 | } |
0 commit comments