Last active
October 24, 2019 01:38
-
-
Save codeboy5/b431918db08250933a08362b6731d36b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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