-
Notifications
You must be signed in to change notification settings - Fork 7
/
StockPriceSpanCalculator.java
41 lines (33 loc) · 1.1 KB
/
StockPriceSpanCalculator.java
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
package monotonic_stack;
import java.util.Stack;
public class StockPriceSpanCalculator {
private final Stack<int[]> stack;
public StockPriceSpanCalculator() {
stack = new Stack<>();
}
public int next(int price) {
int span = 1;
while (!stack.isEmpty() && price >= stack.peek()[0]) {
span += stack.peek()[1];
stack.pop();
}
stack.push(new int[]{price, span});
return span;
}
public static void main(String[] args) {
StockPriceSpanCalculator stockSpanner = new StockPriceSpanCalculator();
boolean passed = true;
passed &= stockSpanner.next(100) == 1;
passed &= stockSpanner.next(80) == 1;
passed &= stockSpanner.next(60) == 1;
passed &= stockSpanner.next(70) == 2;
passed &= stockSpanner.next(60) == 1;
passed &= stockSpanner.next(75) == 4;
passed &= stockSpanner.next(85) == 6;
if (passed) {
System.out.println("All test cases passed!");
} else {
System.out.println("Some test cases failed.");
}
}
}