-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstock_span.cpp
More file actions
48 lines (44 loc) · 830 Bytes
/
Copy pathstock_span.cpp
File metadata and controls
48 lines (44 loc) · 830 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int* stockSpan(int *price, int size) {
stack <int> s;
int *arr=new int[size];
arr[0]=1;
if(size==1)
{
return arr;
}
s.push(0);
for(int i=1;i<size;i++)
{
while(!s.empty() and price[i]>price[s.top()])
{
s.pop();
}
if(s.empty())
{
arr[i]=i+1;
}
else
{
arr[i]=i-s.top();
}
s.push(i);
}
return arr;
}
int main() {
int size;
cin >> size;
int *input = new int[size];
for (int i = 0; i < size; i++) {
cin >> input[i];
}
int *output = stockSpan(input, size);
for (int i = 0; i < size; i++) {
cout << output[i] << " ";
}
cout << "\n";
delete[] input;
delete[] output;
}