-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStockSpan.java
More file actions
43 lines (28 loc) · 744 Bytes
/
StockSpan.java
File metadata and controls
43 lines (28 loc) · 744 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
import java.util.Stack;
import java.util.Arrays;
class StockSpan{
public class EthCode {
static void calculateSpan(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.empty() && price[st.peek()] <= price[i])
st.pop();
S[i] = (st.empty()) ? (i + 1) : (i - st.peek());
st.push(i);
}
}
static void printArray(int arr[]) {
System.out.print(Arrays.toString(arr));
}
public static void main(String[] args) {
int price[] = { 10, 4, 5, 90, 120, 80 };
int n = price.length;
int S[] = new int[n];
System.out.println();
calculateSpan(price, n, S);
printArray(S);
}
}
}