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 rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

题解

我觉得这题还挺难的,是我还没有深刻get的思想

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         int ans = 0, size = height.size(), i = 0;
 5         stack<int>q;
 6         while (i < size) {
 7             if (q.empty()||height[i]<=height[q.top()]) {
 8                 q.push(i++);
 9                 continue;
10             }
11             else {
12                 int now = q.top(); q.pop();
13                 if (q.empty())continue;
14                 ans += (min(height[i], height[q.top()]) - height[now])*(i - q.top() - 1);
15             }
16         }
17         return ans;
18     }
19 };

原文地址:https://www.cnblogs.com/yalphait/p/10351713.html

时间: 2024-11-09 00:59:51

19.2.4 [LeetCode 42] Trapping Rain Water的相关文章

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

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是无意义的(因此要另开

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时

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(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].

刷题42. Trapping Rain Water

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

[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