-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockSpan.java
More file actions
61 lines (52 loc) · 1.79 KB
/
StockSpan.java
File metadata and controls
61 lines (52 loc) · 1.79 KB
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
package learn.ds.stack;
import java.util.Arrays;
import java.util.Stack;
/**
* @author Varma Penmetsa
* The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate
* span of stock’s price for all n days.
*
* The span of the stock’s price on a given day is defined as the maximum number of consecutive days just before the given day,
* for which the price of the stock on the current day is less than or equal to its price on the given day.
*
* For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85},
* then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}
*
* https://www.geeksforgeeks.org/the-stock-span-problem/
*/
public class StockSpan {
//Naive Approach O(n^2)
public static void checkSpan(int price[], int n, int S[]) {
S[0] = 1;
for (int i = 1; i < n; i++) {
S[i] = 1;
for (int j = i - 1; j >= 0; j--) {
if (price[i] >= price[j]) {
S[i]++;
} else {
break;
}
}
}
}
//Using Stack O(n)
public static void checkSpan2(int price[], int n, int S[]) {
Stack<Integer> st = new Stack<>();
st.push(0);
S[0] = 1;
for (int i = 1; i < n; i++) {
while (!st.isEmpty() && price[st.peek()] <= price[i]){
st.pop();
}
S[i] = (st.empty())? (i + 1) : (i - st.peek());
st.push(i);
}
}
public static void main(String[] args) {
int price[] = {10, 4, 5, 90, 120, 80};
int n = price.length;
int S[]= new int[n];
checkSpan2(price, n, S);
System.out.println(Arrays.toString(S));
}
}