方法一:
class StockSpanner {
// 存储某处股票位置以及该股票价格的跨度
Map<Integer, Integer> map;
// 存储某处股票位置以及对应的股票价格
List<Integer> list;
int i = 0;
public StockSpanner() {
map = new HashMap<>();
list = new ArrayList<>();
}
public int next(int price) {
list.add(price);
int idx = i - 1;
int sum = 1;
while(idx >= 0 && list.get(idx) <= price) {
sum += map.get(idx);
idx -= map.get(idx);
}
map.put(i, sum);
i++;
return sum;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/