Leetcode 42题 接雨水(Trapping Rain Water) Java语言求解
题目链接
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语言求解的更多相关文章
- [Swift]LeetCode42. 接雨水 | Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 42. Trapping Rain Water (JAVA)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- [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 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- [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 ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
随机推荐
- 不知如何优选达人?火山引擎 VeDI 零售行业解决方案一键解决!
技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 "人-货匹配"这句营销老话,在直播电商兴起的这几年,似乎不再专指消费者与商品之间的关系. 过去 ...
- Interceptor Handle 执行顺序
preHandle 调用时间:Controller方法处理之前 执行顺序:链式Intercepter情况下,Intercepter按照声明的顺序一个接一个执行 若返回false,则中断执行,注意:不会 ...
- 测试如何定位判断是前端的bug还是后端bug
测试如何定位判断是前端的bug还是后端bug 软件测试工程师的职责是发现BUG,此外,如何体现个人价值,只是提出问题而不去解决,问题就永远得不到闭环.所以,一个资深的测试人员的基本功应该是这样的:深挖 ...
- 使用BAPI_NETWORK_COMP_*实现生产订单组件的增删改查
1.文档说明 对于生产订单组件的增删改有多种办法,比较常用的有使用内部函数CO_XT_COMPONENT_*,有改造BAPI_ALM_ORDER_MAINTAIN来实现,各有千秋. 本文档介绍,通过P ...
- Canal 组件简介与 vivo 帐号实践
互联网应用随着业务的发展,部分单表数据体量越来越大,应对服务性能与稳定的考虑,有做分库分表.数据迁移的需要,本文介绍了vivo帐号应对以上需求的实践. 一.前言 Canal 是阿里巴巴开源项目,关于什 ...
- 用ArcGIS模型构建器生成、导出Python转换空间坐标系的代码
本文介绍在ArcMap软件中,通过创建模型构建器(ModelBuilder),导出地理坐标系与投影坐标系之间相互转换的Python代码的方法. 在GIS领域中,矢量.栅格图层的投影转换是一个经 ...
- 安全情报 | Pypi再现窃密攻击投毒
概述 悬镜安全自研的开源组件投毒检测平台通过对主流开源软件仓库(包括Pypi.NPM.Ruby等)发布的组件包进行持续性监控和自动化代码安全分析,同时结合专家安全经验复审,能够及时发现组件包投毒事件并 ...
- 看这个视频,4万人学会云上部署 Stable Diffusion
目前大火的 AIGC 领域中, 除了 ChatGPT,Stable Diffusion 在文生图领域大放异彩,深刻影响着绘画.视频制作等相关领域.<动手吧,开发者>本期活动邀请 B 站知识 ...
- Python实现PowerPoint(PPT/PPTX)到PDF的批量转换
如果需要处理大量的PPT转PDF的工作,一个个打开并另存为PDF是非常费时的做法.我们可以利用Python编程语言的强大的工具来自动化这个过程,使得批量转换变得简单而高效.本文将介绍如何使用Pytho ...
- 每天学五分钟 Liunx 0011 | 服务篇:进程
1. 进程 程序放在硬盘中,在运行它的时候加载到内存,在内存里程序以进程的方式运行,进程有唯一的 ID ,叫 PID. 写个简单的 Hellow world 程序,让它产生 PID: [root@ ...