leetcode 第41题 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!

题目意思应该很好理解,就是给定数组后,如图,能存放多少单位的水。网上大多数人的方法都是累加每柱能容纳的水。例如。我的做法是,累加当前到之后第一个不小于自己的柱子能存的水。定义一个start,每次计算完一块水域之后,start跳到这块水域的右边柱子往后找新的水域。直到最后。应该是O(n)的。现在就出现问题了,如果出现4 2 3 的例子,如果按照刚才的思路,4的时候,后面没有大于等于4的数,那start就++的话,答案最终是零了。解决的办法是,如果之后没有发现比大于等于自己的柱子,那么就将自己减一,再判断,知道自己为零,或者是找到不小于自己的柱子为止。

oj上证明我的方法比别人快5倍。

class Solution {
public:
    int trap(int A[], int n)
    {
        int sum = 0, start = -1;
        while(++start < n && A[start] == 0);// 找到第一个非零为start
        while(start < n)
        {
            int next = start, minus = 0;
            while(++next < n && A[next] < A[start])
            {
                minus += A[next];
            }
            if (next == n) // 说明之后没有比当前大或者相等的数,所以要将A[start]值减一后再判断,直到有找到不小于当前的数或者已经减到0了
            {
                A[start] -= 1;
                if (A[start] > 0)//只要大于零就要再判断之后有没有不小于当前的数
                    continue;
            }
            sum += (next - start - 1) * A[start] - minus;
            start = next;
        }
        return sum;
    }
};

发现oj上如果把注释去掉,居然更快。。。

时间: 2024-11-15 15:54:20

leetcode 第41题 Trapping Rain Water的相关文章

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven 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,

[leetcode]Trapping Rain Water @ Python

原题地址:https://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 it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,

[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 2012-03-10]

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] 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 th

Trapping Rain Water leetcode java

题目: 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: Trapping Rain Water [041]

[题目] 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 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