Skip to content

Instantly share code, notes, and snippets.

@codeboy5
Last active October 24, 2019 01:38
Show Gist options
  • Save codeboy5/b431918db08250933a08362b6731d36b to your computer and use it in GitHub Desktop.
Save codeboy5/b431918db08250933a08362b6731d36b to your computer and use it in GitHub Desktop.
#include<iostream>
#include<stack>
using namespace std;
void calcSpan(int price[],int n,int *span) {
stack<int> s;
for (int i=0;i<n;i++) {
while (!s.empty() && price[s.top()] <= price[i]) {
s.pop();
}
if (s.empty()) {
span[i] = i+1;
} else {
span[i] = i - s.top();
}
s.push(i);
}
}
int main () {
int price[] = { 10,60,70,65,80,85 };
int n = 6;
int* span = new int[n];
for (int i=0;i<n;i++) {
span[i] = 0;
}
calcSpan(price,n,span);
for (int i=0;i<n;i++) {
cout<<span[i]<<" ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment