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

Sliding Window

The document describes a sliding window template for solving problems involving finding subarrays or substrings that meet certain conditions. It provides examples of problems that can be solved using this template involving finding the longest subarray with 1s after deleting an element, finding the number of substrings containing all characters, finding the minimum length subarray with a sum greater than or equal to a target, and finding the number of non-empty subarrays with a given sum. The template maintains a start and end pointer of the window, tracks counts or sums within the window, and returns the maximum or minimum result.

Uploaded by

shubham kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Sliding Window

The document describes a sliding window template for solving problems involving finding subarrays or substrings that meet certain conditions. It provides examples of problems that can be solved using this template involving finding the longest subarray with 1s after deleting an element, finding the number of substrings containing all characters, finding the minimum length subarray with a sum greater than or equal to a target, and finding the number of non-empty subarrays with a given sum. The template maintains a start and end pointer of the window, tracks counts or sums within the window, and returns the maximum or minimum result.

Uploaded by

shubham kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sliding window:try to create a window if condition satify then increment right if

not satisfy then increment left abd try to maintain the window.
Template for all problem:

int s=0;
int e=0;
int cnt=0;
int ans=0;
for(;e<nums.size();e++){

calculation to be done

for(;condition for invalid window;s++){

Calculation

return ans;
}

Types of problem:

Basic:
Longest Subarray of 1's After Deleting One Element

1493:Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1's in the resulting array.
Return 0 if there is no such subarray.
class Solution {
public:
int longestSubarray(vector<int>& nums) {
int s=0;
int e=0;
int cnt=0;
int ans=0;
for( e=0;e<nums.size();e++){
if(nums[e]==0){
cnt++;
}
for(;cnt>1;s++){
if(nums[s]==0){
cnt--;
}

}
ans=max(ans,e-s+1);
}
return ans-1;

}
};

Using hashmap:

1358. Number of Substrings Containing All Three Characters

Given a string s consisting only of characters a, b and c.


Return the number of substrings containing at least one occurrence of all these characters a, b
and c.

class Solution {
public:
int numberOfSubstrings(string s) {
int si=0,e=0,ans=0;
map<char,int>mp;
for(;e<s.size();e++){
mp[s[e]]++;

for(;mp['a']>=1&&mp['b']>=1&&mp['c']>=1;si++){
ans+=s.size()-e;
mp[s[si]]--;
}

return ans;
}
};
One with prefix sum:

Given an array of positive integers nums and a positive integer target, return the minimal length
of a
Subarray whose sum is greater than or equal to target. If there is no such subarray, return 0
instead.

Input: target = 7, nums = [2,3,1,2,4,3]


Output: 2

class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int s=0;
int e=0;
int ans=INT_MAX;
bool flag=false;
int sum=0;
for(;e<nums.size();e++){
sum+=nums[e];

for(;sum>=target;s++){
flag=true;
ans=min(ans,e-s+1);
sum-=nums[s];
}

}
if(!flag){
return 0;
}
return ans;

}
};
A different varient when we find all the possible subarray from given goal to 0 and
minus that with all total subarray from goal-1 to 0 so we can find the subarray for
given goal exact.

930.Given a binary array nums and an integer goal, return the number of non-empty subarrays with
a sum goal.
A subarray is a contiguous part of the array.
Input: nums = [1,0,1,0,1], goal = 2
Output: 4

class Solution {
private:
int array(vector<int>& nums, int goal){
int s=0,e=0;
int sum=0;
int ans=0;
for(;e<nums.size();e++){
sum+=nums[e];
for(;sum>goal&&s<=e;s++){
sum=sum-nums[s];
}
ans+=e-s+1;
}
return ans;
}
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
return array(nums,goal)-array(nums,goal-1);
}
};

You might also like