Skip to content

Commit cccb59e

Browse files
first negative in window size k along with 3 LC
1 parent 29bf46a commit cccb59e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

23queue/08first_negative.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<bits/stdc++.h>
2+
//find first negative in window of size k
3+
using namespace std;
4+
int main(){
5+
vector<int> v {0,-1,-2,3,4,-5,6,7,-8};
6+
int k = 2; //window size
7+
//find all negative index and fill in queue
8+
queue<int> q;
9+
for(int i=0;i<v.size();i++){
10+
if(v[i]<0) q.push(i);
11+
}// q = {1,2,5,8}
12+
13+
vector<int> ans;
14+
for(int i=0;i<=v.size()-k;i++){
15+
// in case start of window is aheead of idx in queue
16+
while(q.size()>0 && q.front()<i) q.pop();
17+
// // in case end of window is aheead of idx in queue
18+
if(q.size()==0 || q.front()>=i+k) ans.push_back(0);
19+
else{
20+
int idx = q.front();
21+
ans.push_back(v[idx]);
22+
}
23+
}
24+
for(int ele:ans){
25+
cout<<ele<<" ";
26+
}
27+
}

23queue/08first_negative.exe

186 KB
Binary file not shown.

0 commit comments

Comments
 (0)