leetcode—hot100 接雨水

题目:42. 接雨水

困难

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 

示例 2:

输入:height = [4,2,0,3,2,5]
输出:9

提示:

  • n == height.length
  • 1 <= n <= 2 * 104
  • 0 <= height[i] <= 105

题目核心思想:从该点找到起点到该点的最大高度(左边),该点到尾部的最大高度(右边),

并取两边的最小值,即为积水的最大高度

 暴力解法 O(n^2)

class Solution {
    public int trap(int[] height) {
    int len=height.length;
    if(len==0)return 0;
    int res=0;
    for (int i=0;i<len;i++){
        int leftmax=0,rightmax=i;
        for(int j=0;j<=i;j++)if(height[j]>height[leftmax])leftmax=j;
        for(int j=i;j<len;j++)if(height[j]>height[rightmax])rightmax=j;
        res+=Math.min(height[leftmax],height[rightmax])-height[i];
    }
    return  res;
    }
 }   

 动态规划 O(n) 通过left_max[ ],right_max[ ]提前记录该点左边的最大值,和右边的最大值,

省去for循环遍历,节点向左/右寻找最大值的复杂度

class Solution {
    public int trap(int[] height) {
    int len=height.length;
    if(len==0)return 0;
    int res=0;
    int []left_max =new  int[len];
    int []right_max=new int[len];
    left_max[0]=height[0];
    right_max[len-1]=height[len-1];
    for(int i=1;i<len;i++){
        left_max[i]=Math.max(left_max[i-1],height[i]);
    }
    for (int i=len-2;i>=0;i--){
        right_max[i]=Math.max(right_max[i+1],height[i]);
    }
    for(int i=0;i<len;i++){
        res+=Math.min(left_max[i],right_max[i])-height[i];
    }
    return  res;
    }
}

单调栈 O(n) 建立单调递减栈

import javax.naming.AuthenticationNotSupportedException;
import java.util.Stack;

class Solution {
    public int trap(int[] height) {
    int len=height.length;
    if(len==0)return 0;
    int res=0;
    Stack<Integer> stack=new Stack<>();
    int i=0;
    while (i<len){
        while (!stack.empty()&&height[i]>height[stack.peek()]){
            int top=stack.pop();
            if(stack.empty())break;
            int distance=i-stack.peek()-1;
            res+=distance*(Math.min(height[stack.peek()],height[i])-height[top]);
        }
        stack.push(i++);
    }
    return  res;
    }
}

双指针算法 O(n) 从两端遍历数组,左右两指针分别指向左边高度最大,右边高度最大

import javax.naming.AuthenticationNotSupportedException;
import java.util.Stack;

class Solution {
    public int trap(int[] height) {
    int len=height.length;
    if(len==0)return 0;
    int res=0;
    int left=0,right=len-1;
    int left_max=height[0],right_max=height[len-1];
    while (left<right){
        if(height[left]<height[right]){
            if(left_max<height[left]){
                left_max=height[left];
            }
            else{
                res+=left_max-height[left];
                left++;
            }
        }
        else{
            if(right_max<height[right]){
                right_max=height[right];
            }
            else{
                res+=right_max-height[right];
                right--;
            }
        }
    }
    return  res;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值