-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack901.java
More file actions
50 lines (44 loc) · 1.18 KB
/
Stack901.java
File metadata and controls
50 lines (44 loc) · 1.18 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
package stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @ProjectName: leetcode
* @Package: stack
* @ClassName: Stack901
* @Author: markey
* @Description:
* @Date: 2019/10/18 0:24
* @Version: 1.0
*/
public class Stack901 {
/**
* 执行用时 :142 ms, 在所有 java 提交中击败了82.40%的用户
* 内存消耗 :70.5 MB, 在所有 java 提交中击败了95.00%的用户
*/
class StockSpanner {
List<Integer> prices;
Stack<Integer> stack;
int today;
public StockSpanner() {
prices = new ArrayList<>();
stack = new Stack<>();
today = 0;
}
public int next(int price) {
prices.add(price);
while (!stack.isEmpty() && prices.get(stack.peek()-1) <= price) {
stack.pop();
}
int firstMax = stack.isEmpty() ? 0 : stack.peek();
today++ ;
stack.push(today);
return today - firstMax;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/
}