Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 901. Online Stock Span #115

Open
Woodyiiiiiii opened this issue May 11, 2022 · 0 comments
Open

Leetcode 901. Online Stock Span #115

Woodyiiiiiii opened this issue May 11, 2022 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

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

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

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);
 */

参考资料:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant