Hard 42题 Trapping Rain Water
来源:互联网 发布:mac文件存放在哪里 编辑:程序博客网 时间:2024/05/01 22:10
Question:
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!
Solution:
public class Solution { public int trap(int[] height) { int secHeight=0; int left=0; int right=height.length-1; int area=0; while(left<right) { while(left<right && height[left]<=height[right]) { secHeight=Math.max(secHeight,height[left]); area+=secHeight-height[left]; left++; } while(left<right && height[left]>height[right]) { secHeight=Math.max(secHeight,height[right]); area+=secHeight-height[right]; right--; } } return area; }}
0 0
- Hard 42题 Trapping Rain Water
- Trapping Rain Water [hard]
- [Leetcode 42, Hard] Trapping Rain Water
- [hard]42. Trapping Rain Water
- 42. Trapping Rain Water[hard]
- 42Trapping Rain Water
- 42-Trapping Rain Water
- Hard-题目7:42. Trapping Rain Water
- Leetcode 42. Trapping Rain Water (Hard) (cpp)
- [leetcode 42] Trapping Rain Water
- leetcode || 42、 Trapping Rain Water
- [LeetCode 42]Trapping Rain Water
- LeetCode(42) Trapping Rain Water
- Leetcode 42: Trapping Rain water
- LeetCode---(42)Trapping Rain Water
- leetcode 42: Trapping Rain Water
- LeetCode-42 Trapping Rain Water
- Leetcode#42||Trapping Rain Water
- html表单属性
- css_背景
- 错过不再有
- eclipse CDT 开发的一些小问题解决方案,不断更新中...
- 大图滚动--这是精髓实例
- Hard 42题 Trapping Rain Water
- 单独一个窗口 画线,但是分段颜色不同 mt5
- SQLite的安装和使用(一)
- 怎样在Qt下连接读写sqlite数据库
- Git(Github) 001 介绍和下载安装图文教程 For Windows
- 第十四章 C++中的代码重用(1)
- Android 应用资源(一)
- 【LeetCode】455. Assign Cookies (java实现)
- CMake Error: The following variables are used in this project, but they are set to NOTFOUND.