leetcode — trapping-rain-water
/**
* 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的更多相关文章
- [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 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: Given n non-negative integers represe ...
- 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 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
随机推荐
- 第八周助教工作总结——NWNU李泓毅
1.助教博客链接: https://www.cnblogs.com/NWNU-LHY/ 2.作业要求博客链接: http://www.cnblogs.com/nwnu-daizh/p/10687492 ...
- 解决Chrome 70及以上版本的证书问题:Failed to load resource: net::ERR_CERT_SYMANTEC_LEGACY
1.桌面必须要有Chrome 快捷方式 2.进入快捷方式属性 3.修改目标为:"C:\Program Files (x86)\Google\Chrome\Application\chrome ...
- 走进JDK(十一)------LinkedHashMap
概述LinkedHashMap 继承自 HashMap,在 HashMap 基础上,通过维护一条双向链表,解决了 HashMap 不能随时保持遍历顺序和插入顺序一致的问题.除此之外,LinkedHas ...
- 获取mp3文件的采样率
/** * 获取mp3文件的采样率 * @param filefullname 文件完整路径 * @return 采样率 */public int getMp3SampleRate(String fi ...
- laravel 对接 UCenter 基础
说明:1,运行环境 laravel 5.3 php7+nginx+mysql 2,使用安装包 https://github.com/goodspb/laravel5-ucenter 上面有对接方 ...
- [f]智能获取浏览器版本UA信息的方法
var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ...
- 基于UML的高校教务管理系统的设计与实现
一.基本信息 标题:基于UML的高校教务管理系统的设计与实现 时间:2018 出版源:南昌航空大学 领域分类:教育信息化:教务管理系统:UML:SSH:Oracle 二.研究背景 问题定义:高校教务管 ...
- U-Boot Makefile分析(4)具体子Makefile的分析
前面分析的都是多数Makefile要读入的文件,这次我们以drivers/mtd/nand/Makefile为例,分析一个具体的子Makefile是如何工作的. 子Makefile的结构是固定的: i ...
- 【ProtoBuffer】windows上安装ProtoBuffer3.1.0 (附已编译资源)
------- 17.9.17更新 --- 以下这些方法都是扯淡,对我的机器不适用,我后来花了最后成功安装并亲测可用的方法不是靠vs编过的,vs生成的库引入后函数全部报undefine refere ...
- ES6中的箭头函数和普通函数有什么区别?
1.普通函数中的this总是指向调用它的那个对象, 箭头函数没有自己的this,他的this永远指向其定义环境,任何方法都改变不了其指向,如call().bind().apply().(正是因为它没有 ...