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!
public class Solution { public int trap(int[] height) { if (height == null || height.length == 0) { return 0; } int res = 0; int[] left = new int[height.length]; int[] right = new int[height.length]; left[0] = height[0]; int max = height[0]; for (int i = 1; i < height.length; i++) { left[i] = Math.max(max, height[i]); max = Math.max(max, height[i]); } right[height.length-1] = height[height.length-1]; max = height[height.length - 1]; for (int i = height.length - 2; i >= 0; i--) { right[i] = Math.max(max, height[i]); max = Math.max(max, height[i]); } for (int i = 1; i < height.length-1; i++) { int area = Math.min(left[i], right[i]) - height[i]; if (area > 0) { res += area; } } return res; } }
相关推荐
js js_leetcode题解之42-trapping-rain-water.js
c语言入门 C语言_leetcode题解之42-trapping-rain-water.c
Trapping Rain Water 刷题顺序 刷题内容总的来说包括数据结构、算法和技巧 按照tag分类别进行刷题,跳过like<200>like的题目 按Acceptance由高到低进行,每个tag所刷题目大于20(对于类型较少的题目大于5),则进入下...
在本压缩包中,我们关注的是一个Python编程相关的学习资源,特别针对LeetCode平台上的第42题“接雨水”(Trapping Rain Water)的面试题解。LeetCode是一个广泛被程序员用来提升算法技能和准备面试的在线平台,其中...
接下来,我们转向LeetCode的第42题“接雨水”(Trapping Rain Water)。这是一道典型的二维数组问题,考察的是动态规划和双指针技巧。 题目描述:给定一个高度不一的数组代表地面,雨水会积累在低点。求能容纳的...
Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels...
5. 字符串处理问题:例如Candy(糖果)、Trapping Rain Water(接雨水)、Reverse Words in a String II(翻转字符串中的单词)等,考察候选人对字符串操作的熟练度。 6. 动态规划问题:如Minimum Size Subarray ...
Trapping Rain Water 85. Maximal Rectangle Monotonic stack for 2d array 239. Sliding Window Maximum 255. Verify Preorder Sequence in Binary Search Tree 907. Sum of Subarray Minimums 二叉搜索树 99. ...
Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡[LeetCode 145. Binary Tree Postorder Traversal], Tree/stack 2017.06.20 打卡[LeetCode 107. Binary Tree Level Order Traversal II], Tree/BFS ...
力码解决方案 Leetcode是一个网站,人们——...├── Trapping Rain Water │ ├── Readme.md │ └── solution.js ├── Wildcard Matching │ ├── Readme.md │ └── solution.js ├── Valid Number │
- **2.1.15 Trapping Rain Water** - 计算雨水能够困住的水量。 - 实现思路:使用动态规划方法,记录每条柱子左侧最高值和右侧最高值,然后计算水量。 - **2.1.16 Rotate Image** - 将一个正方形矩阵顺时针旋转...
- Trapping Rain Water: 给定一个数组,其中每个元素代表一个宽度为1的柱子高度,计算能接多少雨水。 - Multiply Strings: 给定两个非负整数字符串形式的数,要求进行乘法运算。 - Wildcard Matching: 实现支持'?'和...
Trapping Rain Water Integer to English Words Regular Expression Matching Merge K Sorted Lists Remove Invalid Parentheses Serialize and Deserialize Binary Tree Minimum Window Substring C++ STL中常见...
Trapping Rain Water **知识点:** - **问题描述:** - 给定一个数组表示高度,计算能储存多少雨水。 - **解决方案分析:** - **动态规划:** - 计算每个位置左边最高和右边最高的值。 - 遍历数组,对于每个...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
二、trapping rain water 雨水收集问题 问题描述 给定一个 number 组成的数组,值代表箱子的高度,下雨后,这些箱子中的间隙一共能收集到多少雨水 解答 暴力破解: 遍历数组,找当前 index 往左的最大值和当前 index...
其中第42题,"接雨水"(Trapping Rain Water)是一道经典的二维数组处理问题,涉及到深度优先搜索(DFS)、广度优先搜索(BFS)、栈、队列以及动态规划等算法知识。在这个问题中,我们被要求计算一个二维数组表示的...
多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth Node From End of List Swap Nodes ...Trapping Rain Water