LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD
题目:接雨水
难度:hard
题目内容:
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.

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!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
翻译:简单翻译下就是,给出每一格木块的数量,最后求得这些木块放在一起时能接住多少雨水。
我的思路:此题和第11题很像:Container With Most Water,但是11题里是竖线,这里是方块,而且那是寻求所接水最多的,这里是求所能接住的所有水,不过取水的思想可以参考一下,就是利用双指针,同向移动。每次移动两边比较矮的那一个。
然而,因为考虑到要减去方体的体积,我还是不会做这个。
答案代码:
public int trap(int[] height) {
if (height.length == 0) return 0;
int left = 0;
int right = height.length - 1;
int area = 0, leftHeight = height[0], rightHeight = height[height.length - 1];
while (left < right){
if (height[left] < height[right]){
left++;
leftHeight = Math.max(leftHeight, height[left]);
area += leftHeight - height[left];
}
else{
right--;
rightHeight = Math.max(rightHeight, height[right]);
area += rightHeight - height[right];
}
}
return area;
}
答案复杂度:O(N)
答案思路:果然,是利用了双指针,每次移动比较矮的那一边。
然后,重点来了!
如果移动后的当前高度比移动的这一方的最高的高度要矮,那么储水的量就要加上他们之间的这个差值。
举个例子:(以【】代替方块,___代表0个方块)
【】
【】 【】
【】_________【】【】
A B C
首先因为A比C高,所以移动C那边的指针right,right移动到B点,然后取 high[right] (B)与 之前的最高高度 C 比较,得到还是C高,而因为这边是矮的迟早对面会比这个高,所以此时储水量就应该加上(C-B)。【每移动一格储水量加上移动的这一格能接的水】
而如果当前移动后就是最高的高度,那么储水量则不增加。
所以最后每次移动后,储水量应该加上 ( 此方向的最高高度 - 当前高度 ),对应代码: area += leftHeight - height[left];
本题代码结构和11题Container With Most Water还是有些区别:
1. 多了两边的最高高度记录(eg.rightHeight);
2.本题是先比较左右高矮,移动之后再计算移动产生的盛水量,11题是每次先计算了当前总盛水量再比较看移动那一边。
LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD的更多相关文章
- 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode][Java] Trapping Rain Water
题意: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [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 ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
随机推荐
- 复制新项目 ,tomcat部署时名字还是旧项目名
基于一个就项目 copy 成新项目 关于项目名注意点: 在工作空间下 copy一份新项目 1首先在目录将项目名字更改. 2.在新项目下 找到 [.project]文件 将里面的nama更改 3 ...
- Python菜鸟之路:Django 序列化数据
类型一:对于表单数据进行序列化 这时需要用到ErrorDict. ret['errors'] = obj.errors.as_data() result = json.dumps(ret, cls=J ...
- 解决 apt-get the following packages has unmet dependencies 问题
安装vpn遇到以下问题: 显示flinux print util和openconnect存在依赖库的冲突 此时尝试安装新的tk.vpnc-scripts.libopenconnect5,尝试apt-g ...
- chrome命令
chrome://settings(设置) chrome://extensions(扩展程序) chrome://history(历史记录) chrome://settings/clearBrowse ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
- Harbor实现容器镜像仓库的管理和运维
本次分享主要讲述了在开发运维中的管理容器镜像方法.为了便于说明原理,较多地使用Harbor作为例子. 内容主要包括: 开发和生产环境中镜像仓库的权限控制: 镜像远程同步(复制)的原理: 大规模应用镜像 ...
- nginx缓存原理
一.HTTP字段理解 1.Expires: 该字段的http1.0时的规范,值为一个绝对时间的GMT格式的时间字符串,代表缓存资源的过期时间,在这个时点之前即命中缓存. 缺点:服务器返回的时间,可能与 ...
- day4 字符串的使用方法
一.字符串切片 索引和切片 [起始位置:结束位置:步长] s1 = 'python全栈8期' # 索引从0开始[索引(下标,index)] print(s1[0]) print(s1[3]) prin ...
- ipv6被拒的解决方法
A,检测服务器是否支持ipv6连接 用mac 搭建IPv6环境测试,只能测试客户端到mac这段网络正常,但是mac到服务器还是IPv4的,所以没有办法测试服务器的IPv6是否正常.可以用手机打开2)步 ...
- php 内存泄漏
所谓内存泄漏是指进称在执行过程中,内存的占有率逐步升高,不释放, 系统所拥有的可用内存越来越少的现象. php-fpm耗光内存,不释放,就是所谓的内存泄漏,内存泄漏对长期运行的程序有威胁,所以应该定期 ...