【LeetCode题意分析&解答】42. Trapping Rain Water
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
.
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!
题意分析:
给定一个非负整数的list,每一个元素代表如图中柱状图的纵坐标。结合图中来看,黑色的是给定的list,让你求出这些黑色柱体围起来能够盛的水量是多少,也就是图中蓝色柱体的面积(每个柱体宽都是1)。
解答:
本题和11. Container With Most Water有些类似,那道题求的是两个坐标之间能够盛的最大水量,本题是所有坐标中能够盛下的水量。考虑到每个坐标宽度都是1,所以我们把问题简化为求每个坐标上面能够盛的水量。
那么单个坐标的水量怎么求呢?我们假设i为当前需要求的坐标,如果i左侧比i高且是最高的设为left_max,同时能够保证i右侧有比left_max高的坐标,那么i能够盛的水量就是left_max减去i。基于这样一种思路,我们依然可以用双指针的方式去解。
AC代码:
class Solution(object):
def trap(self, height):
ret_num = 0
left, right, left_max, right_max = 0, len(height) - 1, 0, 0
while left <= right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
ret_num += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
ret_num += right_max - height[right]
right -= 1
return ret_num
【LeetCode题意分析&解答】42. Trapping Rain Water的更多相关文章
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
随机推荐
- org.hibernate.QueryException: could not resolve property: address of:
Hibernate: select count(*) as y0_ from test.course this_ org.hibernate.QueryException: could not res ...
- Request和Response详解
转自:http://zhidao.baidu.com/link?url=8BI0cjlcFdBSJKHTZlpo874eqtbTJoZfrh3miQgM_05RvSER8skPiBc1wSPZtXT8 ...
- Java异常分类 转载
Java异常分类 http://blog.csdn.net/woshixuye/article/details/8230407 一.基本概念 看java的异常结构图 Throwable是所有异 ...
- Manacher 算法
Manacher算法用于求回文子串,它的复杂度为O(n). 这个算法有一个很巧妙的地方,它把奇数的回文串和偶数的回文串统一起来考虑了.在相邻的两个字符之间加进一个分隔符 '#' ,串的首尾也要加. 原 ...
- C语言队列的实现
队列是常用的数据结构之一,下面给出一个链式队列的实现: 头文件Queue.h #ifndef Queue_H #define Queue_H typedef int Item; typedef str ...
- 【JQ成长笔记】jQuery Validate验证插件
validate是一款很好的jq插件,提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同 ...
- Android GreenDao with Android Studio IDE
转:http://blog.surecase.eu/using-greendao-with-android-studio-ide/ In this tutorial we will show you ...
- hdu 2828 Lamp 重复覆盖
题目链接 给n个灯和m个开关, 每个灯可以由若干个开关控制, 每个开关也可以控制若干个灯, 问你能否找到一种开关的状态, 使得所有的灯都亮. 将灯作为列, 然后把每个开关拆成两行, 开是一行, 关是一 ...
- spoj ONP - Transform the Expression 中缀转后缀
题目链接 将中缀表达式转化为后缀表达式. 数字的话直接放到答案的字符串里. 如果是左括号就进栈, 右括号就让栈里的符号都出来直到第一个左括号. 否则的话比较当前符号的优先级和栈顶符号的优先级. #in ...
- Latex命令笔记
1.\documentclass[hyperref, UTF8]{ctexart} 2.\numberwithin{equation}{section} %article中让公式按章节名编号 3.\p ...