题目链接

https://leetcode-cn.com/problems/trapping-rain-water/

题目内容

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例:

输入: [0,1,0,2,1,0,1,3,2,1,2,1]

输出: 6

思路

我们可以设置两个数组,left_height_array和right_height_array;

其中left_height_array表示当前柱子前面的所有柱子的最高值(不包括当前柱子);

right_height_array表示当前柱子后面的所有柱子的最高值(不包括当前柱子);

取left_height_array[i]与right_height_array[i]两个值的最小值,如果相等则取这个相同的值,存储在res数组中

在height[i]处可以存的水量的求法:

如果res[i]的值比height[i]大,当前凹处可以存res[i] - height[i]的水;

如果res[i]的值不比height[i]大,不能存水;

再把res[i]累加便可以得到最终存水量。

(其实可以在比较left_height_array[i] 与 right_height_array[i] 大小关系时就可以进行累加存水量了。)

案例分析

height : [0,1,0,2,1,0,1,3,2,1,2,1]

先求解left_height_array[i]从前往后算:

第一个元素左边没有元素,所以left_height_array[0] = 0;

第二个元素左边元素有0,左边最高的是0,所以left_height_array[1] = 0;

第三个元素左边元素有0,1,左边最高的是1,所以left_height_array[2] = 1;

第四个元素左边元素有0,1,0,左边最高的是1,所以left_height_array[3] = 1;

第五个元素左边元素有0,1,0,2,左边最高的是2,所以left_height_array[4] = 2;

第六个元素左边元素有0,1,0,2,1左边最高的是2,所以left_height_array[5] = 2;

第七个元素左边元素有0,1,0,2,1,0,左边最高的是2,所以left_height_array[6] = 2;

第八个元素左边元素有0,1,0,2,1,0,1左边最高的是2,所以left_height_array[7] = 2;

第九个元素左边元素有0,1,0,2,1,0,1,3左边最高的是3,所以left_height_array[8] = 3;

第十个元素左边元素有0,1,0,2,1,0,1,3,2左边最高的是3,所以left_height_array[9] = 3;

第十一个元素左边元素有0,1,0,2,1,0,1,3,2,1左边最高的是3,所以left_height_array[10] = 3;

第十二个元素左边元素有0,1,0,2,1,0,1,3,2,1,2左边最高的是3,所以left_height_array[11] = 3;

所以left_height_array数组为

0,0,1,1,2,2,2,2,3,3,3,3

同理可求得right_height_array数组为

3,3,3,3,3,3,3,2,2,2,1,0;

height数组为:

0,1,0,2,1,0,1,3,2,1,2,1

index=0时,left_height_array[0] = 0,right_height_array[0] = 0不能放水,存水量为0;

index=1时,left_height_array[1] = 0,right_height_array[1] = 3不能放水,两者最小为0,存水量为0;

index=2时,left_height_array[2] = 1,right_height_array[1] = 3不能放水,两者最小为2,而height[i]为0,存水量为两者最小值 - height[i] = 1;

以此类推;

最后把它们都相加即可。

代码

class Solution {
public int trap(int[] height) {
int result = 0;
if(height.length == 0) return result;
int[] left_height_array = new int[height.length];
int[] right_height_array = new int[height.length];
int left_max = 0;
int right_max = 0;
left_height_array[0] = 0;
right_height_array[0] = 0;
for(int i = 1;i<height.length;i++){
if(height[i-1]>left_max){
left_max = height[i-1];
}
left_height_array[i] = left_max;
} for(int i = height.length -2;i>=0;i--){
if(height[i+1]>right_max){
right_max = height[i+1];
}
right_height_array[i] = right_max;
}
for(int i=0;i<height.length;i++){
if(left_height_array[i] == right_height_array[i]){
if(left_height_array[i] > height[i])
result+=left_height_array[i]-height[i]; }else if(left_height_array[i] < right_height_array[i]){
if(left_height_array[i] > height[i])
result+=left_height_array[i]-height[i];
}else{
if(right_height_array[i]>height[i])
result += right_height_array[i] - height[i];
}
}
return result;
}
}

欢迎关注

扫下方二维码即可关注,微信公众号:code随笔

Leetcode 42题 接雨水(Trapping Rain Water) Java语言求解的更多相关文章

  1. [Swift]LeetCode42. 接雨水 | Trapping Rain Water

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

  2. 42. Trapping Rain Water (JAVA)

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

  3. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  4. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

  6. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  7. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  8. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

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

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

  10. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

随机推荐

  1. python 升级后 yum 无法使用 File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: `/usr/libexec/urlgrabber-ext-down`

    原因为升级python后新建了软连接指向了新版本,除非同时升级yum不然无法使用.需要手动更改报错文件指向新版本后即可解决. [root@localhost pdserving]# yum insta ...

  2. CentOS7 Docker 安装,配置国内镜像

    删除已有Docker sudo yum remove docker \ docker-ce \ docker-client \ docker-client-latest \ docker-common ...

  3. Cypress系列(1)-Window下安装Cypress并打开

    Cypress系列(1)-Window下安装Cypress并打开 系统要求 Cypress是一个被安装在你电脑上的桌面应用,你的操作系统需要满足如下条件才能正常安装 Mac OS 10.9+(仅提供6 ...

  4. 神经网络优化篇:详解指数加权平均的偏差修正(Bias correction in exponentially weighted averages)

    指数加权平均的偏差修正 \({{v}_{t}}=\beta {{v}_{t-1}}+(1-\beta ){{\theta }_{t}}\) 在上一个博客中,这个(红色)曲线对应\(\beta\)的值为 ...

  5. 国内申请微软新必应(New Bing)

    国内申请微软新必应(New Bing) 本文解决了两个问题: 1 需国外网络环境 2 点击加入候补名单无限返回错误 注册outlook邮箱 https://outlook.live.com/ 一步一步 ...

  6. 2019 篇 - 分享数百个 HT的工业互联网 2D 3D 可视化应用案例

    继<分享数百个 HT 工业互联网 2D 3D 可视化应用案例>2018 篇,图扑软件定义 2018 为国内工业互联网可视化的元年后,2019 年里我们与各行业客户进行了更深度合作,拓展了H ...

  7. 【驱动】SPI驱动分析(一)-SPI协议简介

    1. 什么是SPI SPI全拼Serial Peripheral interface(串行外围设备接口),是由Motorola(摩托罗拉)在MC68HCXX系列处理器上定义的,主要应用于EEPROM( ...

  8. 实现不限层级的Element的NavMenu

    做管理后台开发的时候,需要用到Element的NavMenu组件,于是乎,翻开文档,大致是这样实现的. <el-menu> <el-menu-item index="1&q ...

  9. P1077-DP【黄】

    昨天好几道题没做出来很郁闷,结果今天上来半小时不到就直接做出一道黄DP题了,不错,又有写题的冲动了. 这道题我一直被那个"因为方案数可能很多,请输出方案数对 1000007取模的结果.&qu ...

  10. Nacos源码 (6) Grpc概述与Nacos集成

    Nacos 2.x版本增加了GRPC服务接口和客户端,极大的提升了Nacos的性能,本文将简单介绍grpc-java的使用方式以及Nacos中集成GRPC的方式. grpc-java GRPC是goo ...