LeetCode OJ-- Trapping Rain Water*

https://oj.leetcode.com/problems/trapping-rain-water/

模拟题,计算出在凹凸处存水量。

对于一个位置 i ,分别计算出它左边的最大值 left (从左扫描一遍), 右边的最大值 right(从右扫描一遍) 。找left right中的最小值,如果大于 A[i],则做 min - A[i] 的累加。

class Solution {
public:
    int trap(int A[], int n) {
        if(n<=1)
            return 0;

        vector<int> leftHigher;
        vector<int> rightHigher;
        int leftLarge = 0, rightLarge = 0;
        for(int i = 0;i<n;i++)
        {
            if(A[i]>leftLarge)
                leftLarge = A[i];
            leftHigher.push_back(leftLarge);
        }
        rightHigher.resize(n);
        for(int j = n-1;j>=0;j--)
        {
            if(A[j]>rightLarge)
                rightLarge = A[j];
            rightHigher[j] = rightLarge;
        }

        int sum = 0;
        for(int i = 0;i<n;i++)
        {
            int min = leftHigher[i]<rightHigher[i]?leftHigher[i]:rightHigher[i];
            if(min>A[i])
                sum += min - A[i];
        }

        return sum;
    }
};

LeetCode OJ-- Trapping Rain Water*

时间: 2024-11-13 03:36:47

LeetCode OJ-- Trapping Rain 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

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 elevatio

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[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

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 a

LeetCode: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 【 Trapping Rain Water 】python 实现

题目: 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