-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockPricePeaks.cpp
66 lines (49 loc) · 1 KB
/
stockPricePeaks.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<stack>
using namespace std;
int * getPeaks(int a[], int size);
int * getPeaksUsingStack(int a[], int size);
int main() {
int size = 5;
int arr[] = { 6, 3, 4, 5, 2 };
int *p;
int i;
//using simple brute force method
//p = getPeaks(arr, size);
//using stack
p = getPeaksUsingStack(arr, size);
//print the output
for (i = 0; i < 5; i++) {
cout << arr[i] << " peaks of :" << *(p + i) << " days" << endl;
}
return 0;
}
int * getPeaks(int a[], int size) {
static int s[5];
int i, j;
for (i = 0; i < size; i++) {
j = 1;
while (j <= i && a[i] >= a[i - j])
j++;
s[i] = j;
}
return s;
}
int * getPeaksUsingStack(int a[], int size) {
//variable to store peaks value
static int s[5];
//a stack to store precedence highest value
stack <int> st;
int p,i;
//let calculate the peaks
for (i = 0; i < size; i++) {
while (!st.empty() && a[i] > st.top())
st.pop();
if (st.empty())
s[i]=i+1;
else
s[i] = i-st.top();
st.push(i);
}
return s;
}