/**
* 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. 字符串、数组、对象常用API

    常用的字符串API  1.常见方法和属性 length 属性,获取字符串的字符数量 charAt(i) 返回给定位置的字符 charCodeAt( ) 返回给定位置的字符的字符编码 <scrip ...

  2. SVN用法及常见问题分析

    SVN中英文对比: 1,今天遇到的新问题,在父节点里面找不到子节点文件夹,在子节点里面可以上传但是却一直上传不上去. 具体原因:子文件夹里面有个.svn文件(打开隐藏的项目可见),是的子文件夹的svn ...

  3. Shiro与Spring整合

    Shiro引入Spring 添加jar包/maven配置 <!-- shiro支持 --> <dependency> <groupId>org.apache.shi ...

  4. sql批量新增,修改

    <insert id="insertExtDocList" parameterType="map"> INSERT INTO extprjdoc ( ...

  5. Unity编辑器扩展-Custom List, displaying data your way

    本文转自http://catlikecoding.com/unity/tutorials/editor/custom-list/ Custom List, displaying data your w ...

  6. APIView源码简单分析图

    APIView源码简单分析 !声明:下面这个dispatch分发方法不在是父类View里的dispatch了,APIView重新封装了这个dispatch.(整个核心就是initialize_requ ...

  7. JS模块化工具require.js教程(二):基本知识

    前一篇:JS模块化工具我们以非常简单的方式引入了requirejs,这一篇将讲述一下requirejs中的一些基本知识,包括API使用方式等 基本API require会定义三个变量:define,r ...

  8. 利用Python+163邮箱授权码发送邮件

    背景 前段时间写了个自动打卡的脚本,但是脚本不够完善,我需要知道,打卡到底成没成功,因此,我想到了用Python执行完代码之后,再执行一段发送邮件的代码.需求开始明确了,就开始分析和写代码实现吧. 分 ...

  9. ETC(电子不停车收费系统)的发展演变

    ETC引进中国是在上世纪的90年代中期,当时中国部分经济发达地区的高速公路车流量激增,从而导致了收费口的交通堵塞.高速公路堵车现象时有发生,拥堵严重的路段可能会天天堵,有时候一堵好几天.高速公路管理手 ...

  10. Lesson 22 A glass envelope

    Text My daughter, Jane, never dreamed of receiving a letter from a girl of her own age in Holland. L ...