LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/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 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 1:
从左到右扫描,计算到从左边到curr的最高的bar,从右到左扫描,计算到从右边到curr的最高的bar。
再扫描一次,把这两者的低者作为{桶}的高度,如果这个桶高于A[i]的bar,那么A[i]这个bar上头可以存储height - A[i]这么多水。把这所有的水加起来即可。
public class Solution {
public int trap(int[] A) {
if (A == null) {
return ;
} int max = ; int len = A.length;
int[] left = new int[len];
int[] right = new int[len]; // count the highest bar from the left to the current.
for (int i = ; i < len; i++) {
left[i] = i == ? A[i]: Math.max(left[i - ], A[i]);
} // count the highest bar from right to current.
for (int i = len - ; i >= ; i--) {
right[i] = i == len - ? A[i]: Math.max(right[i + ], A[i]);
} // count the largest water which can contain.
for (int i = ; i < len; i++) {
int height = Math.min(right[i], left[i]);
if (height > A[i]) {
max += height - A[i];
}
} return max;
}
}
2015.1.14 redo:
合并2个for 循环,简化计算。
public class Solution {
public int trap(int[] A) {
// 2:37
if (A == null) {
return ;
} int len = A.length;
int[] l = new int[len];
int[] r = new int[len]; for (int i = ; i < len; i++) {
if (i == ) {
l[i] = A[i];
} else {
l[i] = Math.max(l[i - ], A[i]);
}
} int water = ;
for (int i = len - ; i >= ; i--) {
if (i == len - ) {
r[i] = A[i];
} else {
// but: use Math, not max
r[i] = Math.max(r[i + ], A[i]);
} water += Math.min(l[i], r[i]) - A[i];
} return water;
}
}
CODE:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/Trap.java
LeetCode: Trapping Rain Water 解题报告的更多相关文章
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [leetcode]Trapping Rain Water @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- Leetcode: Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Trapping Rain Water II 题解
题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
随机推荐
- eclipse properties文件插件
eclipse properties插件 CreateTime--2018年4月22日22:51:34 Author:Marydon 下载地址:properties文件插件.rar 1.将plug ...
- 〖Linux〗OK6410a蜂鸣器的驱动程序编写全程实录
最近在看一本书,受益匪浅,作者是李宁,下边是编写本次蜂鸣器的全程实录: 1. 了解开发板中的蜂鸣器 1) 查看蜂鸣器buzzer在底板中的管脚信息 2) 查看蜂鸣器在总线中的信息 3) 翻看S3C64 ...
- python中的多态
# -*- coding: cp936 -*- #python 27 #xiaodeng #python中的多态 #多态:一个操作的意义取决于被操作对象的类型,相同的消息给予不同的对象会引发不同的动作 ...
- 【laravel5.4】使用baum\node 类库实现无限极分类
1.在model中引入baum\node 类库,并继承,具体参考 https://packagist.org/packages/baum/baum 2.核心代码: /* * model::create ...
- Ajax Control Toolkit 34个服务器端控件的使用
摘自:http://blog.csdn.net/yaoshuyun/article/details/2218633 1. Accordion[功能概述] Accordion可以让你设计多个panel ...
- PHP-数据库永久连接
以下为PHP官网上对数据库永久连接做的解释: 永久的数据库连接是指在脚本结束运行时不关闭的连接.当收到一个永久连接的请求时.PHP 将检查是否已经存在一个(前面已经开启的)相同的永久连接.如果存在,将 ...
- Java IDE选择,常用Java IDE排行榜
Java IDE众多,java开发主要用.最多用.国内较流行.本人常用的java IDE如下: 开发java大项目的IDE一般都用eclipse或netbeans(几乎我所在的公司都是在用eclips ...
- RHEL7 -- NetworkManager
RHEL7中默认的网络服务是由NetworkManager提供,NetworkManager可以动态控制和配置网络. 网络工具和应用 应用或工具 描述 NetworkManager 默认的网络守护进程 ...
- DataGridView合并单元格(多行多列合并)
一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...
- Shlwapi.h头文件的使用
// TestShlwAPI.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <Shlwapi.h>#pragma ...