leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,

Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

思路:此题咋一看简单,但是细细思考,越想越复杂,感觉无从下手,无奈想了一天没搞定,只能求助网上资料,终于思路如下:(网友很强大)

(参考网址:http://www.xuebuyuan.com/1586534.html)

最后黑体字的Thanks Marcos的意思是让我们放大我们的想象力,因为上图很容易误导我们的。如果只有上图的情况的话,我们可以很好用贪心法解决,但是,用贪心法是会出错的,因为如果我们计算当前最低点,然后往两边扩展找到两边递增的最高点,那么这个最高点很可能是局部最高点,答案就会出错。

有两个解题思路:

1 两边往中间搜索

2 由左往右搜索,跳跃式计算

如下面的分析图,会比较直观:

蓝色代表水,可以看见很多局部最高点都被水淹了。

这里计算面积不用一般几何书的方法,这里是两边往中间遍历,记录当前第二高点secHight,然后利用这个第二高点减去当前历经的柱子,剩下就装水容量了。

为什么是第二高点?因为两边比较,最高的点不用动,只移动第二高点。

第一个思路,按照上图的思想就能写出非常简洁的程序了,时间复杂度为O(n):

本人照着上面的思路写的代码如下:

public class Solution {
    public int trap(int[] height) {
        Stack<Integer> st = new Stack<Integer>();
        if(height.length == 0)
            return 0;
        int i = 0;
        int j = height.length - 1;
        int ans = 0;//返回的答案
        int secHight = 0;//第二个高度(最高的那个不动)
        while(i < j){
            if(height[i] < height[j]){
                secHight = Math.max(secHight,height[i]);
                //因为长度为1,高度也就是面积值,如果height[i]==secHight,则新增面积为0
                ans += secHight - height[i];
                i++;
            }else{
                secHight = Math.max(secHight,height[j]);
                //因为长度为1,高度也就是面积值,如果height[i]==secHight,则新增面积为0
                ans += secHight - height[j];
                j--;
            }
        }
        return ans;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 02:23:01

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法的相关文章

19.2.4 [LeetCode 42] Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of

LeetCode 42 Trapping Rain Water(积水体积)

题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意图.求解积水最大体积. 首先对所给数组进行遍历操作,求出最大高度所对应的下标为maxIndex 之后从左向右进行遍历,设置左边高度为leftMax 并初始化为 height[0],从i==1到i==maxIndex进行遍历.不断更新water,以及leftMax 当height[I]小于leftMax时

LeetCode 42. Trapping Rain Water

首先说说最初的但是不正确的思路,stk存值,初始化为输入数组的第一位.从左往右读入数组每一位,如果height[i]>stk.top(),说明stk.top()无法蓄水,pop掉,替换为height[i]:如果height[i]<=stk.top(),那么从i开始遍历,直到找到大于等于stk.top()的height[i]或者直到最后一位,找的时候维护变量tmp存蓄水的量,但是如果没有找到大于等于stk.top()的height[i]的话,实际上是无法蓄水的,也就是tmp是无意义的(因此要另开

No.42 Trapping Rain Water

1 class Solution 2 { 3 public: 4 int trap(vector<int> &height) 5 {/* 6 雨水覆盖问题: 7 每一个bar能承受的水量为:它左侧所有中最高的和右侧所有中最高的中取最小值作为一个瓶颈[否则也留不住], 8 若该值大于当前bar的高度,其差值即为所求 9 累加所有bar能承受的水量即为所求 10 法三:[左右两个指针] 11 找到最高的位置,将数组分为两部分: 12 对左侧数据,因为右边已经有了最高值,所以,类似法一,找到

刷题42. Trapping Rain Water

一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的解法 这个题目是找"坑",然后计算里面可以存的"雨".总共提交了5次,前面几次都是边界错误. 代码如下: #include<iostream> #include<vector> using namespace std; class Solutio

42. Trapping Rain Water(js)

42. Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://oj.leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing an elevation map where the width of each bar is 1,compute how much water

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha