/**
* Source : https://oj.leetcode.com/problems/trapping-rain-water/
*
* Created by lverpeng on 2017/7/15.
*
* 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.
*
* ^
* |
* 3 | +--+
* | | |
* 2 | +--+xxxxxxxxx| +--+xx+--+
* | | |xxxxxxxxx| | |xx| |
* 1 | +--+xxx| +--+xxx+--+ | +--+ +--+
* | | |xxx| | |xxx| | | | | | |
* 0 +---+--+---+--+--+---+--+--+--+--+--+--+----->
* 0 1 0 2 1 0 1 3 2 1 2 1
*
* 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 TrappingRainWater { /**
* 找到数组组成的柱条之间存水的单位数
*
* 先找到最大的,分为左右两边
* 依次遍历左边,将当前值与左边最大的值比较,如果大于左边最大值则更新左边最大值,计算该位置可以存的水量
* 右边类似
*
*
* @param num
*/
public int trap (int[] num) {
int max = 0;
int maxIndex = 0;
for (int i = 0; i < num.length; i++) {
if (max < num[i]) {
max = num[i];
maxIndex = i;
}
} // 当前位置以前的较大值
int preMax = 0;
int result = 0;
// 计算左边
for (int i = 0; i < maxIndex; i++) {
if (preMax < num[i]) {
preMax = num[i];
}
result += preMax - num[i];
} // 计算右边
int afterMax = 0;
for (int i = num.length - 1; i > maxIndex ; i--) {
if (afterMax < num[i]) {
afterMax = num[i];
}
result += afterMax - num[i];
} return result;
} public static void main(String[] args) {
TrappingRainWater trappingRainWater = new TrappingRainWater();
int[] arr = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trappingRainWater.trap(arr));
}
}

leetcode — trapping-rain-water的更多相关文章

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

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  4. 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 ...

  5. [leetcode]Trapping Rain Water @ Python

    原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...

  6. Leetcode Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] Trapping Rain Water 栈

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  9. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

  10. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

随机推荐

  1. Cache高速缓冲存储器

    Cache的命中率:命中Cache的次数比总访问次数 平均访问时间:t(Cache)X命中次数+t(未命中)X未命中次数 Cache与主存的映射方式: 直接映射 全相联映射 组相联映射 图片来源:ht ...

  2. jdango

    1.jdango的下载 命令行: pip install django ==1.11.18 pip install django ==1.11.18 -i https://pypi.douban.co ...

  3. 阿里云,未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接.

    阿里云主机使用SQL Server作为数据库服务器,连接数据库时候出现错误. 按照网上经验,检查SQL服务是否开启,sa用户权限,数据库安全性和连接权限: 关闭服务器防火墙,修改入站规则: 检查阿里云 ...

  4. Android基础知识学习

    IPC  (Inter-Process Communication) 意思是: 进程间的通信,是指两个进程之间进行数据交换的过程. Android中如何开启多进程呢? 只需要给四大组件(Activit ...

  5. angular-指令

    ng-app 作用域 ng-init 声明 module 模块 ng-model 双向绑定 ng-bind 绑定 angular是一个MVC框架:即 M------------------module ...

  6. 浅析http缓存

    1.什么是缓存 将服务器上的静态资源,保存在本地,当发送web请求的时候,如果本地有“已缓存”的静态资源,则从使用本地保存的静态资源,而不是从源原服务器再次请求. 2.缓存的优点 缓存减少冗余的数据传 ...

  7. attention 介绍

    前言 这里学习的注意力模型是我在研究image caption过程中的出来的经验总结,其实这个注意力模型理解起来并不难,但是国内的博文写的都很不详细或说很不明确,我在看了 attention-mech ...

  8. 删除坏掉的 Active Directory Domain

    最近公司的某个 Domain Controller 报告可能由于长时间没在线,同步失败,然后用 Repldiag 工具清理 lingering objects 的过程中,该工具报告存在 serious ...

  9. 背水一战 Windows 10 (114) - 后台任务: 后台任务的 Demo(与 app 不同进程), 后台任务的 Demo(与 app 相同进程)

    [源码下载] 背水一战 Windows 10 (114) - 后台任务: 后台任务的 Demo(与 app 不同进程), 后台任务的 Demo(与 app 相同进程) 作者:webabcd 介绍背水一 ...

  10. Codeforces Round #554 (Div. 2) 1152A - Neko Finds Grapes

    学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152A - Neko Finds Grapes 题目链接:"https://codeforces. ...