0% found this document useful (0 votes)
9 views

SLIDING WINDOW

The document outlines various sliding window problems and their solutions, including finding the maximum sum subarray of size K, identifying the first negative integer in every window, and counting occurrences of anagrams. It also covers algorithms for determining the longest subarray with a given sum, the longest substring with K unique characters, and the minimum window substring. Each problem is linked to its respective coding challenge for further reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SLIDING WINDOW

The document outlines various sliding window problems and their solutions, including finding the maximum sum subarray of size K, identifying the first negative integer in every window, and counting occurrences of anagrams. It also covers algorithms for determining the longest subarray with a given sum, the longest substring with K unique characters, and the minimum window substring. Each problem is linked to its respective coding challenge for further reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SLIDING WINDOW

1. Maximum Sum Subarray of size K

https://www.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1

2. First negative in every window of size K (Queue would work)

https://www.geeksforgeeks.org/problems/first-negative-integer-in-every-
window-of-size-k3345/1
3. Count of occurrences of anagrams(fixed window)

https://www.geeksforgeeks.org/problems/count-occurences-of-
anagrams5839/1

4. Maximum element in every window(lc-239)

1. Step that will take place in any way: Insert array element in the deque, but
pop all the smaller elements.
2. If window size is hit: Add the first element, and before shifting ‘i’ check, if it
is present in deque.
https://www.geeksforgeeks.org/problems/maximum-of-all-subarrays-of-size-
k3101/1

5. Longest subarray with sum K(positives)

1. Step that will take place in any way: Add element to sum.
2. If sum==k: Update answer
3. If sum exceeds k: keep subtracting until it exceeds, then check, whether
now it has got equal to k

https://www.naukri.com/code360/problems/longest-subarray-with-sum-
k_6682399?leftPanelTabValue=PROBLEM
6. Longest Substring with K uniques - (no need to remove key)

https://www.geeksforgeeks.org/problems/longest-k-unique-characters-
substring0853/1
7. Longest substring without repeating characters

https://www.geeksforgeeks.org/problems/longest-distinct-characters-in-
string5848/1
8. Fruit into Baskets(lc-904)
Note: Here we get a possible answer at <=2, not ==2

public int totalFruit(int[] fruits){


HashMap<Integer,Integer> map = new HashMap<>();
int i = 0, j = 0;
int n = fruits.length;
int mapSize = 0;
int maxFruits = Integer.MIN_VALUE;
while(j<n){
int key1 = fruits[j];
int value1 = map.getOrDefault(key1,0)+1;
map.put(key1,value1);
if(value1==1) mapSize++;

if(mapSize<=2){
maxFruits = Math.max(maxFruits,j-i+1);
j++;
}
else if(mapSize>2){
while(mapSize>2){
int key2 = fruits[i];
int value2 =
map.getOrDefault(key2,0)-1;
map.put(key2,value2);
if(value2==0) mapSize--;
i++;
}
if(mapSize==2){
maxFruits = Math.max(maxFruits,j-
i+1);
}
j++;
}
}
return maxFruits;

9. Minimum Window Substring(leetcode 76)


public String minWindow(String s1, String s2){
HashMap<Character,Integer> map = new HashMap<>();
int i = 0, j = 0;
int n = s1.length();
String result = "";
int smallestLength = Integer.MAX_VALUE;
for(int k=0; k<s2.length(); k++){
char key1 = s2.charAt(k);
int val1 = map.getOrDefault(key1,0)+1;
map.put(key1,val1);
}
int mapSize = map.size();

while(j<n){
if(map.containsKey(s1.charAt(j))){
char key2 = s1.charAt(j);
int val2 = map.get(key2)-1;
map.put(key2,val2);
if(val2==0) mapSize--;
}
if(mapSize>0) j++;
else if(mapSize==0){
if(j-i+1<smallestLength){
smallestLength = j-i+1;
result = s1.substring(i,j+1);
}
while(mapSize==0){
if(map.containsKey(s1.charAt(i))){
char key3 = s1.charAt(i);
int val3 = map.get(key3)+1;
map.put(key3,val3);
if(val3==1){
if(j-i+1<smallestLength){
smallestLength = j-i+1;
result =
s1.substring(i,j+1);
}
mapSize++;
}
}
i++;
}
j++;
}

}
if(smallestLength == Integer.MAX_VALUE) return "";
return result;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy