-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidingWindow.cpp
More file actions
289 lines (278 loc) · 6.46 KB
/
Copy pathslidingWindow.cpp
File metadata and controls
289 lines (278 loc) · 6.46 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include<bits/stdc++.h>
using namespace std;
//count occurences of anagrams GFG
//txt = forxxorfxdofr pat = for 3
bool allZero(vector<int>&counter){
for(int i = 0; i < counter.size(); i++){
if(counter[i]!=0){
return false;
}
}
return true;
}
int search(string &pat, string &txt){
int n = txt.size();
vector<int> counter(26, 0);
for(int i = 0; i < pat.length(); i++){
counter[pat[i]-'a']++;
}
int i = 0;
int j = 0;
int cnt = 0;
while(j < n){
counter[txt[j]-'a']--;
if(j-i+1 == pat.length()){
if(allZero(counter)){
cnt++;
}
counter[txt[i]-'a']++;
i++;
}
j++;
}
return cnt;
}
//Find all anagrams in a string Leetcode 438
//s = "cbaebabacd", p = "abc" [0,6]
bool allZero(vector<int>&counter){
for(int i = 0; i < counter.size(); i++){
if(counter[i]!=0){
return false;
}
}
return true;
}
vector<int> search1(string &pat, string &txt){
int n = txt.size();
vector<int> counter(26, 0);
for(int i = 0; i < pat.length(); i++){
counter[pat[i]-'a']++;
}
int i = 0;
int j = 0;
vector<int>result;
while(j < n){
counter[txt[j]-'a']--;
if(j-i+1 == pat.length()){
if(allZero(counter)){
result.push_back(i);
}
counter[txt[i]-'a']++;
i++;
}
j++;
}
return result;
}
//minimum size subarray sum Leetcode 209
int minSubArrayLen(vector<int>& nums, int target){
int minLen = INT_MAX;
int sum = 0;
int i = 0;
int j = 0;
while(j < nums.size()){
sum+=nums[j];
j++;
while(sum >= target){
minLen = min(minLen, sum);
sum-=nums[i];
i++;
}
}
return minLen == INT_MAX ? 0 : minLen;
}
//minimum window substring Leetcode 76
string minWindow(string s, string t){
if(s.length()<t.length()) return "";
unordered_map<char, int> mpp;
for(char &ch : t){
mpp[ch]++;
}
int windowSize = INT_MAX;
int start = 0;
int i = 0;
int j = 0;
int counter = t.length();
while(j < s.length()){
char ch = s[j];
if(mpp[ch]>0){
counter--;
}
mpp[ch]--;
while(counter == 0){
int currWindow = j - i + 1;
if(currWindow < windowSize){
start = i;
windowSize = currWindow;
}
mpp[s[i]]++;
if(mpp[s[i]]>0){
counter++;
}
i++;
}
j++;
}
return (windowSize == INT_MAX)? "" : s.substr(start, windowSize);
}
//Count subarrays with fixed bounds Leetcode 2444
//smaller = min(minPosi, maxPosi)
//temp = smaller - culpritIdx
//if(temp<=0) ans += 0
//else ans += temp
//valid subarrays ending at i=max(0,min(minPosi,maxPosi)−culpritIdx)
long long countSubbarays(vector<int>&nums, int minK, int maxK){
long long ans = 0;
int minPosi = -1;
int maxPosi = -1;
int culpritIdx = -1;
for(int i = 0; i < nums.size(); i++){
if(nums[i]==minK){
minPosi = i;
}
if(nums[i]==maxK){
maxPosi = i;
}
if(nums[i]<minK || nums[i]>maxK){
culpritIdx = i;
}
long long smaller = min(minPosi, maxPosi);
long long temp = smaller - culpritIdx;
ans += (temp<=0)?0:temp;
}
return ans;
}
//Maximum number vowels in a substring length Leetcode 1456
bool isVowel(char ch){
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
return true;
}
return false;
}
int maxVowels(string s, int k){
int i = 0;
int j = 0;
int count = 0;
int maxV = 0;
while(j < s.length()){
if(isVowel(s[j])){
count++;
}
if(j-i+1 == k){
maxV = max(maxV, count);
if(isVowel(s[i])){
count--;
}
i++;
}
j++;
}
return maxV;
}
//K Radius Subarray Averages lc 2090
vector<int> getAverages(vector<int>& nums, int k){
int n = nums.size();
if(k == 0){
return nums;
}
vector<int>result(n, -1);
if(n < 2*k+1){
return result;
}
int l = 0;
int r = 2*k;
int i = k;
long long window = 0;
for(int j = l; j <= r; j++){
window = window + nums[j];
}
int avg = window/(2*k+1);
result[i] = avg;
i++;
r++;
while(r < n){
int out = nums[l];
int in = nums[r];
window = window+in-out;
result[i] = window/2*k+1;
i++;
r++;
l++;
}
return result;
}
//1493. Longest Subarray of 1's After Deleting One Element
//Example 1:
// Input: nums = [1,1,0,1]
// Output: 3
// Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
// Example 2:
// Input: nums = [0,1,1,1,0,1,1,0,1]
// Output: 5
// Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
// Example 3:
// Input: nums = [1,1,1]
// Output: 2
// Explanation: You must delete one element.
int findMax(vector<int>&nums, int skip_idx){
int n = nums.size();
int currLen = 0;
int maxLen = 0;
for(int i = 0; i < n; i++){
if(i==skip_idx){
continue;
}
if(nums[i] == 1){
currLen++;
maxLen = max(currLen, maxLen);
}
else{
currLen = 0;
}
}
return maxLen;
}
int longestSubarray(vector<int>& nums) {
int n = nums.size();
int result = 0;
int countZero = 0;
for(int i = 0; i < n; i++){
if(nums[i] == 0){
countZero++;
result = max(result, findMax(nums, i));
}
}
if(countZero == 0) return n-1;
return result;
}
int longestSubarray(vector<int>& nums) {
int i = 0;
int j = 0;
int zeroCount = 0;
int ans = 0;
while(j < nums.size()){
if(nums[j] == 0) zeroCount++;
while(zeroCount > 1){
if(nums[i] == 0) zeroCount--;
i++;
}
ans = max(ans, j-i);
j++;
}
return ans;
}
int longestSubarray(vector<int>& nums) {
int i = 0;
int j = 0;
int idx_pos = -1;
int ans = 0;
while(j < nums.size()){
if(nums[j] == 0){
i = idx_pos+1;
idx_pos = j;
}
ans = max(ans, j-i);
j++;
}
return ans;
}