Skip to content

Instantly share code, notes, and snippets.

@Schachte
Last active April 14, 2024 06:39
Show Gist options
  • Save Schachte/87d7c0165a584f26b3ad7845f8010387 to your computer and use it in GitHub Desktop.
Save Schachte/87d7c0165a584f26b3ad7845f8010387 to your computer and use it in GitHub Desktop.
Sliding Window Maximum Sum Subarray
import java.util.*;
class LongestSubstringKDistinct {
public static int findLength(String str, int k) {
int windowStart = 0, maxLength = 0;
Map<Character, Integer> charFrequencyMap = new HashMap<>();
for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
char rightChar = str.charAt(windowEnd);
charFrequencyMap.put(rightChar, charFrequencyMap.getOrDefault(rightChar, 0) + 1);
while (charFrequencyMap.size() > k) {
char leftChar = str.charAt(windowStart);
charFrequencyMap.put(leftChar, charFrequencyMap.get(leftChar) - 1);
if (charFrequencyMap.get(leftChar) == 0) {
charFrequencyMap.remove(leftChar);
}
windowStart++;
}
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
}
package slidingwindow;
/**
* Find the max sum subarray of a fixed size K
*
* Example input:
* [4, 2, 1, 7, 8, 1, 2, 8, 1, 0]
*
*/
public class MaxSumSubarray {
public static int findMaxSumSubarray(int[] arr, int k) {
int maxValue = Integer.MIN_VALUE;
int currentRunningSum = 0;
for (int i = 0; i < arr.length; i++) {
currentRunningSum += arr[i];
if (i >= k - 1) {
maxValue = Math.max(maxValue, currentRunningSum);
currentRunningSum -= arr[i - (k - 1)];
}
}
return maxValue;
}
public static void main(String[] args) {
System.out.println(findMaxSumSubarray(new int[]{4, 2, 1, 7, 8, 1, 2, 8, 1, 0}, 3));
}
}
package slidingwindow;
public class SmallestSubarrayGivenSum {
public static int smallestSubarray(int targetSum, int[] arr) {
int minWindowSize = Integer.MAX_VALUE;
int currentWindowSum = 0;
int windowStart = 0;
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
currentWindowSum += arr[windowEnd];
while(currentWindowSum >= targetSum) {
minWindowSize = Math.min(minWindowSize, windowEnd - windowStart + 1);
currentWindowSum -= arr[windowStart];
windowStart++;
}
}
return minWindowSize;
}
public static void main(String[] args) {
int[] input = new int[]{4,2,2,7,8,1,2,8,10};
int targetSum = 8;
System.out.println(smallestSubarray(targetSum, input));
}
}
@Kiran777-jpg
Copy link

Kiran777-jpg commented Nov 16, 2022

In LongestSubstringKDistinct solution, I think "we should update the maxLength only if the charFrequencyMap size is equals to k"

if(charFrequencyMap==k)
maxLength = Math.max(maxLength, windowEnd - windowStart + 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment