leetcode 739, 84, 42, 901 (Monotonic Stack)

If a problem is suitable to use the monotonic stack, it must have at least three characters:

  1. It is a “range queries in an array” problem.
  2. The minima/maxima element or the monotonic order of elements in a range is useful to get the answer to every range query.
  3. When an element is popped from the monotonic stack, it will never be used again.

 

Solution for leetcode 739 

https://leetcode.com/problems/daily-temperatures/

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        Stack<Integer> stack = new Stack();
        int[] output = new int[n];
        for(int i = n - 1; i >= 0; i--){
            while(!stack.isEmpty() && temperatures[stack.peek()] <= temperatures[i]){
                    stack.pop();
            }
            if(stack.isEmpty()){
                output[i] = 0;  
            }
            else{
                output[i] = stack.peek() - i;
            }
            stack.add(i);
        }
        return output;
    }
}

Thoughts on leetcode 84

https://leetcode.com/problems/largest-rectangle-in-histogram/

For a height at index i, if we can find its left and right boundaries, we can find its area. By maintaining a monotonic stack, we can find the left and right boundaries when we pop elements from the stack. (it's very important to understand the characteristics)

Solution for leetcode 84

https://leetcode.com/problems/largest-rectangle-in-histogram/

class Solution {
    public int largestRectangleArea(int[] heights) {
        Stack<Integer> stack = new Stack();
        int maxArea = Integer.MIN_VALUE;
        //calculate the area when iterating through the heights array
        for(int i = 0; i < heights.length; i++){
            while(!stack.isEmpty() && heights[stack.peek()] > heights[i]){
                int height = heights[stack.pop()];
                int left = stack.isEmpty()? -1 : stack.peek();      
                int width = i - left - 1;       
                int area = height * width;    
                maxArea = area > maxArea ? area : maxArea;

            }
            stack.add(i);
        }
        //calculate the remaining area when poping the element from the stack
        //the right boundary of the remaining elements in the stack is the end
        while(!stack.isEmpty()){
            int height = heights[stack.pop()];
            int left = stack.isEmpty()? -1 : stack.peek();
            int right = heights.length;
            int width = right - left - 1;
            int area = height * width;
            maxArea = area > maxArea ? area : maxArea;
          
        }
        return maxArea;
    }
}

Thoughts on leetcode 42 

https://leetcode.com/problems/trapping-rain-water/

Solution for leetcode 42

class Solution {
    public int trap(int[] height) {
        Stack<Integer> stack = new Stack();
        int rain = 0;
        for(int i = 0; i < height.length; i++){
            while(!stack.isEmpty() && height[stack.peek()] <= height[i]){
                int low = height[stack.pop()];
                if(!stack.isEmpty()){
                    int prev = height[stack.peek()];
                    int tall = Math.min(prev, height[i]) - low;
                    int width = i - stack.peek() -1;
                    rain += tall * width;
                }
            }
            stack.add(i);
        }
        return rain;
    }
}

Solution for leetcode 901 

https://leetcode.com/problems/online-stock-span/

class StockSpanner {
    Stack<Integer> stack;
    ArrayList<Integer> arr;
    int cur = -1;

    public StockSpanner() {
        stack = new Stack();
        arr = new ArrayList();
    }
    
    public int next(int price) {
        if(stack.isEmpty() || arr.get(stack.peek()) > price){
            cur++;
            stack.add(cur);
            arr.add(price);
            return 1;
        }
        else{
             while(!stack.isEmpty() && arr.get(stack.peek()) <= price){
                stack.pop();
            }
            cur++;
            int prev = stack.isEmpty()? -1 : stack.peek();
            int output = cur - prev;
            stack.add(cur);
            arr.add(price);
            return output;           
        }  
    }
}

内容概要:本文档详细介绍了Android高级控件的使用方法及其应用场景。首先讲解了下拉列表Spinner,包括其两种表现形式(下拉列表形式和对话框形式),并介绍了适配器Adapter的基础概念及其三种主要类型:数组适配器ArrayAdapter、简单适配器SimpleAdapter和基本适配器BaseAdapter,重点阐述了它们各自的特点和使用步骤。接着,文档对列表视图ListView进行了深入探讨,涉及分隔线样式、按压背景等属性的设置方式。随后,描述了网格视图GridView,详细解释了其拉伸模式的效果及取值。对于翻页视图ViewPager,不仅介绍了基本概念,还展示了翻页标签栏PagerTabStrip的具体应用,特别是用于创建启动引导页。最后,文档介绍了碎片Fragment的概念,强调了其在大屏设备上的优势,以及与ViewPager结合使用的实战案例——记账本应用。 适合人群:有一定Android开发基础,希望深入了解并掌握高级控件使用的开发者。 使用场景及目标:①掌握下拉列表、列表视图、网格视图、翻页视图等高级控件的实现细节;②理解适配器的作用及其不同类型的使用场景;③学会使用Fragment优化应用界面布局,提高用户体验;④通过具体案例(如记账本),将所学控件应用于实际开发中。 阅读建议:本文档内容详实,涵盖多种高级控件的理论知识与实践技巧。建议读者在学习过程中结合官方文档或相关资料进行对比研究,同时动手实践,以便更好地理解和掌握这些控件的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值