LeetCode 042 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!

代码如下:

class Solution {
public:
    int trap(int A[], int n) {

        int maxIdx = 0;
        int water = 0;

        //找到最长的木板,设为maxIdx
        for(int i = 1; i < n; i++){
            if(A[i] > A[maxIdx]){
                maxIdx = i;
            }
        }

        int max = A[0];

        //左侧逼近
        for(int i = 1; i < maxIdx; i++){

            if(max < A[i])
                max = A[i];
            //木板左边(max)和右边(最高)都比它高,则可以放
            // max - 该木板长度 的水
            else
                water += max - A[i];
        }

        //右侧逼近
        max = A[n - 1];
        for(int i = n - 2; i >= maxIdx; i--){
            if(max < A[i]) max = A[i];
            else water += max - A[i];
        }

        return water;

    }
};
时间: 2024-11-12 16:04:42

LeetCode 042 Trapping Rain Water的相关文章

[leetcode][042] Trapping Rain Water (Java)

题目在这里: https://leetcode.com/problems/trapping-rain-water/ [标签] Array; Stack; Two Pointers 这个题我感觉很难,我自己是完全不会.下面贴的是别人想到的好方法,自己适当做了些简单调整. 我觉得,这个解法非常巧妙的使用了双向的 two pointers.用到的变量有左右两个指针,一个在最左边,一个在最右边,然后两个一起往中间走.在走的时候维护两个变量,left barrier 和 right barrier,即左边

Java for LeetCode 042 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. 解题思路: 先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,J

[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

Leetcode 动态规划 Trapping Rain Water

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Total Submissions: 50810My Submissions Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much wate

【leetcode】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(凹槽的雨水) 解题思路和方法

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】042 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

LeetCode[Array]: 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