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]. 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题意:给定一个直方图,求出能积水的面积代码如下:
/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function(height) {
    var left=0,right=height.length-1;
    var maxLeft=0,maxRight=0;
    var res=0;
    while(left<right){
        if(height[left]<height[right]){
            if(height[left]>=maxLeft) maxLeft=height[left];
            else res+=maxLeft-height[left] ;
            left++;
        }else{
            if(height[right]>=maxRight) maxRight=height[right];
            else res+=maxRight-height[right];
            right--;
        }
    }
    return res;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10424853.html

时间: 2024-11-08 17:36:16

42. Trapping Rain Water(js)的相关文章

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

一.题目说明 题目是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 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

11 Container With Most Water 42.Trapping Rain Water

11 和 42 本质上都是木桶原理: 11 如何才能存最多的水? 假设 a[left] < a[right] ,    total = a[left] *(right-left) ,  那么 right -1, right-2 位置 都比 total 小, 此时就没必要move right 了, 因为所有的right -x 都比 right 小. 此时只需要move left 指针, 继续找最大value. code 很简单: public int maxArea(int[] height) {

[leedcode 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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

【一天一道LeetCode】#42. Trapping Rain Water

一天一道LeetCode系列 (一)题目 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 m

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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented