Skip to content

Commit ff23947

Browse files
authored
Create stock sPan.cpp
1 parent fbadcf9 commit ff23947

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

z-revision/stock sPan.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<stack>
2+
int* stockSpan(int *arr, int size) {
3+
// Write your code here
4+
int * ans = new int [size];
5+
6+
stack < pair<int , int > > s ;
7+
8+
int index = 0 ;
9+
10+
for(int i = 0 ; i< size ; i++)
11+
{
12+
if(s.size()==0)
13+
{
14+
ans [ index++] = -1;
15+
}
16+
else if( s.top().first >= arr[i])
17+
{
18+
ans[index++] = s.top().second;
19+
}
20+
else
21+
{
22+
while( s.size() and s.top().first< arr[i])
23+
s.pop();
24+
if(s.size()==0)
25+
{
26+
ans [index++] = -1;
27+
}
28+
else if( s.top().first >= arr[i])
29+
{
30+
ans[index++] = s.top().second;
31+
}
32+
}
33+
s.push({ arr[i] , i });
34+
}
35+
for(int i =0 ; i< size ; i++)
36+
ans[i] = i- ans[i];
37+
38+
return ans ;
39+
40+
}

0 commit comments

Comments
 (0)