Repository navigation
Expand file tree
/
Copy pathSlidingWindow.cpp
More file actions
230 lines (193 loc) · 4.65 KB
/
Copy pathSlidingWindow.cpp
File metadata and controls
230 lines (193 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <algorithm>
#include <limits.h>
#include <typeinfo>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
using namespace std;
// Sliding Window Maximum, Fixed window
vector<int> MaxSlidingWindow(vector<int>& nums, int k){
vector<int> ans;
deque<int> dq;
int size= nums.size();
int index=0;
// First window
while(index<k){
while(!dq.empty() && nums[dq.back()]<nums[index]){
dq.pop_back();
}
dq.push_back(index);
index++;
}
ans.push_back(nums[dq.front()]);
// Rest all
while(index<size){
// Removal of out of range element
if(index - dq.front() == k){
dq.pop_front();
}
// Removal of smaller elements
while(!dq.empty() && nums[dq.back()] < nums[index]){
dq.pop_back();
}
// Addition of new elemet
dq.push_back(index);
ans.push_back(nums[dq.front()]);
index++;
}
return ans;
}
// Minimum Size Subarray Sum
int MinSubArrayLen(int target, vector<int> &nums){
int l=0;
int r=0;
int sum= 0;
int count= INT_MAX;
while(r < nums.size()){
sum += nums[r];
// Target found
while(sum >= target){
int curr = r-l+1;
count= min(count, curr);
// Minimizing
sum -= nums[l];
l++;
}
r++;
}
if(count == INT_MAX){
return 0;
}
return count;
}
// Two Sum
vector<int> TwoSum(vector<int> &arr, int target){
// Making a copy of arr, as to return indexes of proper no.
vector<int> nums=arr;
vector<int> ans;
// Sorting Duplicate
sort(nums.begin(), nums.end());
// Finding if sum exists
int start=0;
int end= nums.size() -1;
while(start<end){
int sum= nums[start]+ nums[end];
if(sum== target){
break;
}
else if(sum>target){
end--;
}
else{
start++;
}
}
// Finding indexes
int size= nums.size();
start= nums[start];
end= nums[end];
for(int i=0; i<size; i++){
if(arr[i] == start){
ans.push_back(i);
// 3,3 me same 3 ka index na return karde
start= INT_MIN;
}
else if(arr[i] == end){
ans.push_back(i);
// 3,3 me same 3 ka index na return karde
end=INT_MAX;
}
}
return ans;
}
// Binary Sub Array Sum, 2 Pointers
int BinSubArraySumSolve(vector<int> &nums, int goal){
if(goal < 0){
return 0;
}
if(nums.size() == 1){
return (goal == nums[0]) ? nums[0] : 0;
}
int left= 0;
int right= 0;
int count=0;
int sum=0;
while(right < nums.size()){
sum += nums[right];
if(sum <= goal){
count += right-left+1;
right++;
}
else{
while(sum > goal && left<=right){
sum -= nums[left];
left++;
}
count += right-left+1;
right++;
}
}
return count;
}
int BinSubArraySum(vector<int> &nums, int goal){
return BinSubArraySumSolve(nums, goal) - BinSubArraySumSolve(nums, goal-1);
}
// Minimum Window Substring
string MinWindow(string s, string t){
int size1= s.length();
int size2= t.length();
// Pattern is greater than str
// Invalid case
if(size1 < size2){
return "";
}
// Valid Case
int start= 0;
int ansIndex= -1;
int ansLength= INT_MAX;
int count= 0;
unordered_map<char, int> sMap;
// Pattern Count
unordered_map<char, int> tMap;
for(char ch: t){
tMap[ch]++;
}
for(int i=0; i<size1; i++){
char ch= s[i];
sMap[ch]++;
// Valid Character, Extra pattern wale ch ignore kr dega
if(sMap[ch] <= tMap[ch]){
count++;
}
// Minimize
if(count == size2){
// Extra or unnecesssary characters
while(sMap[s[start]] > tMap[s[start]] || tMap[s[start]] == 0){
if(sMap[s[start]] > tMap[s[start]]){
sMap[s[start]]--;
}
start++;
}
// min fn isnt used because
int winLength= i- start +1;
if(winLength < ansLength){
ansLength= winLength;
ansIndex= start;
}
}
}
if(ansIndex == -1){
return "";
}
return s.substr(ansIndex, ansLength);
}
int main(){
return 0;
}