Trapping Rain Water
来源:互联网 发布:nginx重启命令 编辑:程序博客网 时间:2024/05/21 19:24
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[] A) { Stack<Integer> pre = new Stack<Integer>(); int result = 0; for(int i = 0; i < A.length; i++) { if(pre.isEmpty() || A[pre.peek()] > A[i]) { pre.push(i); continue; } int mid = pre.pop(); if(pre.isEmpty()) pre.push(i); else { int left = pre.peek(); result += (Math.min(A[left], A[i])-A[mid]) * (i-left-1); i--; } } return result; }}
0 0
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- Trapping Rain Water
- Trapping Rain Water
- [LeetCode]Trapping Rain Water
- Trapping Rain Water
- Trapping Rain Water
- LeetCode-Trapping Rain Water
- Trapping Rain Water
- [leetcode] Trapping Rain Water
- Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- 38 - Trapping Rain Water
- 谷歌地图打不开的解决办法
- centos ssh内网登录慢及vsftpd登录慢的解决办法
- 第十课:Activity 及其子类的使用四:配置 Activity
- 路创照明
- Struts ActionForm简单理解
- Trapping Rain Water
- 老鼠走迷宫解析
- 科学艺术服装及纺织品设计
- 【UVA】1220-Party at Hali-Bula?(动态规划,树)
- ./configure参数详解
- Hibernate_15_缓存的使用
- 赫尔利背包
- UVa1347 Tour
- 怎样去掉Eclipse中的Js错误提示