2024/11/29 灵茶题单 单调栈
This commit is contained in:
parent
7327e78f3c
commit
a1caf8fe22
|
@ -0,0 +1,46 @@
|
|||
package com.cool.ling_cha_mount.stack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Num901 {
|
||||
|
||||
}
|
||||
class StockSpanner {
|
||||
|
||||
Deque<Integer> stack=new LinkedList<>();
|
||||
List<Integer> list=new ArrayList<>();
|
||||
public StockSpanner() {
|
||||
stack.push(-1);
|
||||
}
|
||||
|
||||
public int next(int price) {
|
||||
list.add(price);
|
||||
while(stack.peek()>0&&list.get(stack.peek())<=price){
|
||||
stack.pop();
|
||||
}
|
||||
int res=list.size()-1-stack.peek();
|
||||
stack.push(list.size()-1);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
class StockSpanner1 {
|
||||
|
||||
Deque<int[]> stack = new LinkedList<>();
|
||||
private int curDay = -1;
|
||||
|
||||
public StockSpanner1() {
|
||||
stack.push(new int[] { curDay, Integer.MAX_VALUE });
|
||||
}
|
||||
|
||||
public int next(int price) {
|
||||
while (price >= stack.peek()[1]) {
|
||||
stack.pop();
|
||||
}
|
||||
int res = (++curDay) - stack.peek()[0];
|
||||
stack.push(new int[] { curDay, price });
|
||||
return res;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue