Skip to content

Leetcode 901. Online Stock Span #115

@Woodyiiiiiii

Description

@Woodyiiiiiii

这道题也是单调栈的解法。

单调栈看上去很简单,也就是栈内结构有序,但可以通过存储数据结构的多变来实现多种功能。

class StockSpanner {
    
    Deque<int[]> stack;

    public StockSpanner() {
        stack = new LinkedList<>();
    }
    
    public int next(int price) {
        
        int cnt = 1;
        
        while (!stack.isEmpty() && price >= stack.peek()[0]) {
            cnt += stack.pop()[1];
        }
        
        stack.push(new int[]{price, cnt});
        
        return cnt;
        
    }
}

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */

参考资料:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions