zoukankan      html  css  js  c++  java
  • 双指针遍历/滑动窗口 —— 42_接雨水

    6. 42_接雨水
    img
    /*
    上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)
    */
    
    /*
    暴力解, O(n²),O(1)
    */
    class Solution {
        int trap(int[] height)
        {
            int ans = 0;
            int size = height.length;
            for (int i = 1; i < size - 1; i++) {
                int leftMax = 0, rightMax = 0;
                for (int j = i; j >= 0; j--) {
                    leftMax = Math.max(leftMax, height[j]);
                }
                for (int j = i; j < size; j++) { 
                    rightMax = Math.max(rightMax, height[j]);
                }
                ans += Math.min(leftMax, rightMax) - height[i];
            }
            return ans;
        }
    }
    
    /*
    动态编程 O(n),O(n)
    */
    class Solution {
        public int trap(int[] height)
        {
            if (height == null || height.length == 0)
                return 0;
            int ans = 0;
            int len = height.length;
            int[] leftMax = new int[len];
            int[] rightMax = new int[len];
            leftMax[0] = height[0];
            for (int i = 1; i < len; i++) {
                leftMax[i] = Math.max(height[i], leftMax[i - 1]);
            }
            rightMax[len - 1] = height[len - 1];
            for (int i = len - 2; i >= 0; i--) {
                rightMax[i] = Math.max(height[i], rightMax[i + 1]);
            }
            for (int i = 1; i < len - 1; i++) {
                ans += Math.min(leftMax[i], rightMax[i]) - height[i];
            }
            return ans;
        }
    }
    
    /*
    栈的应用, O(n),O(n)(单调递减栈)
    */
    class Solution {
        int trap(int[] height)
        {
            int ans = 0, cur = 0;
            Stack<Integer> st = new Stack<>();
            while (cur < height.length) {
                while (!st.isEmpty() && height[cur] > height[st.peek()]) {
                    int top = st.peek();
                    st.pop();
                    if (st.isEmpty())
                        break;
                    int distance = cur - st.peek() - 1;
                    int boundedHeight = Math.min(height[cur], height[st.peek()]) - height[top];
                    ans += distance * boundedHeight;
                }
                st.push(cur++);
            }
            return ans;
        }
    }
    /*
    双指针,O(n),O(1)
    */
    class Solution {
        int trap(int[] height)
        {
            int left = 0, right = height.length - 1;
            int ans = 0;
            int leftMax = 0, rightMax = 0;
            while (left < right) {
                if (height[left] < height[right]) {
                    if(height[left] >= leftMax)
                        leftMax = height[left];
                    else 
                        ans += (leftMax - height[left]);
                    ++left;
                }
                else {
                    if(height[right] >= rightMax)
                        rightMax = height[right];
                    else 
                        ans += (rightMax - height[right]);
                    --right;
                }
            }
            return ans;
        }
    }
    
  • 相关阅读:
    Eclipse 介绍
    XML 解析之 dom4j 解析器
    XPath 快速入门
    XML 解析之 jaxp 解析器
    XML 之快速入门
    java操作Excel
    识别jar的编译JDK版本
    IntelliJ Idea 工具
    单点登录SSO
    java泛型-类型擦除
  • 原文地址:https://www.cnblogs.com/s841844054/p/13736351.html
Copyright © 2011-2022 走看看