-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStocks.java
More file actions
33 lines (27 loc) · 815 Bytes
/
Stocks.java
File metadata and controls
33 lines (27 loc) · 815 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
import com.sun.source.tree.BreakTree;
import java.util.Stack;
public class Stocks {
public static int[] stockSpan(int[] price) {
int[] span = new int[price.length];
Stack<Integer> s = new Stack<>();
span[0] = 1;
s.push(0);
int i;
for (i = 1; i < price.length; i++) {
while (!s.isEmpty() && price[s.peek()] <= price[i]) {
s.pop();
}
if (s.isEmpty()) {
span[i] = i + 1;
} else {
span[i] = i - s.peek();
}
s.push(i);
}
return span;
}
public static void main(String[] args) {
int[] arr = {60, 70, 80, 100, 90, 75, 80, 120};
System.out.println(stockSpan(arr));
}
}