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 ...
随机推荐
- 9月活动回顾(免费领取PPT)|火山引擎DataLeap、ByteHouse多位专家带来DataOps、实时计算等前沿技术分享!
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 在上月举行的火山引擎开发者社区 Meetup 第12期暨超话数据专场<数智化转型背景下的火山引擎大数据技 ...
- 一个简单的例子看明白 async await Task
测试代码: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using Sys ...
- 如何用 Serverless 一键部署 Stable Diffusion?
思路 其实很简单, 我们只需要将镜像里面的动态路径映射到 NAS文件存储里面即可,利用 NAS 独立存储文件模型,扩展,语言包等,并且我们可以为管理 NAS 单独配置一个可视化的后台,用简单的文件上传 ...
- WebGPU光追引擎基础课:使用WebGPU绘制三角形
大家好~我开设了"WebGPU光追引擎基础课"的线上课程,从0开始,在课上带领大家现场写代码,使用WebGPU开发基础的光线追踪引擎 课程重点在于基于GPU并行计算,实现BVH构建 ...
- vue-awesome-swiper swiper/dist/css/swiper.css 报not found错误
解决办法删除package-lock.json文件写死package.json版本号 "vue-awesome-swiper": "^3.1.3", 删除no ...
- 元素可视区client系列
client翻译过来就是客户端,我们使用client系列的相关属性来获取元素可视区的相关信息. 通过client系列的相关属性可以动态的得到该元素的边框大小.元素大小等. client系列属性 作用 ...
- 软件开发常说的CI/CD是什么
本文翻译自国外论坛 medium,原文地址:本文翻译自国外论坛 medium,原文地址:https://medium.com/gitconnected/basics-of-ci-cd-a98340c6 ...
- 每天学五分钟 Liunx 100 | 存储篇:磁盘分区
这一节主要介绍 Liunx 是怎么用磁盘的. 磁盘分区 在 Liunx 中一切皆文件,磁盘在 Liunx 中也是文件,包括 /dev/hd[a-d](以 IDE 为接口) 和 /dev/sd[a-p] ...
- 存储器(Memory)
存储器(Memory) 通信领域中有很多重要的部分,比如基带.射频芯片,存储器. 1.存储器是什么?和内存如何进行区分? 作为数据的载体,存储器是任何电子设备中都必不可少的单元.由于存储器之间相似的名 ...
- 用CI/CD工具Vela部署Elasticsearch + C# 如何使用
Vela 除了可以帮我们编译.部署程序,利用它的docker部署功能,也能用来部署其他线上的docker镜像,例如部署RabbitMQ.PostgreSql.Elasticsearch等等,便于集中管 ...