当前位置:网站首页>Analysis of rainwater connection
Analysis of rainwater connection
2022-07-06 19:30:00 【Little straw hat】
Title Description
Given n Nonnegative integers indicate that each width is 1 Height map of columns of , Calculate columns arranged in this way , How much rain can be received after rain .
Example 1:
Input :height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output :6
explain : Above is an array of [0,1,0,2,1,0,1,3,2,1,2,1] Height map of representation , under these circumstances , Can connect 6 Units of rain ( The blue part indicates rain ).
Example 2:
Input :height = [4,2,0,3,2,5]
Output :9
source : Power button (LeetCode)
link :https://leetcode.cn/problems/trapping-rain-water
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method : Double pointer
Maximum water intake at each location = The left and right pointer positions correspond to the current maximum height corresponding to the endpoint of the end with small height - The height of the current position
utilize left and right The pointer scans from both ends of the array .
Judge the water receiving volume and pointer movement conditions .
Always give priority to moving the pointer with small height of the left and right pointer positions .
Code
class Solution(object):
def trap(self, height):
""" :type height: List[int] :rtype: int """
ans = 0
left, right = 0, len(height) - 1
leftMax = rightMax = 0
while left < right: // Calculate from the left and right ends
leftMax = max(leftMax, height[left]) // Update the maximum height of the left endpoint
rightMax = max(rightMax, height[right]) // Update the maximum height of the right endpoint
if height[left] < height[right]: // Determine the size of the left endpoint and the right endpoint , Move the small end towards the middle
ans += leftMax - height[left] // Calculate the maximum rainwater that can be connected to the left end point
left += 1
else:
ans += rightMax - height[right] // Calculate the maximum rainwater that can be connected to the right end
right -= 1
return ans
边栏推荐
- Leetcode topic [array] - 119 Yang Hui triangle II
- swagger2报错Illegal DefaultValue null for parameter type integer
- An error occurs when installing MySQL: could not create or access the registry key needed for the
- PMP practice once a day | don't get lost in the exam -7.6
- 学习探索-无缝轮播图
- 10 schemes to ensure interface data security
- Problems encountered in using RT thread component fish
- LeetCode-1279. 红绿灯路口
- In depth analysis, Android interview real problem analysis is popular all over the network
- [translation] supply chain security project in toto moved to CNCF incubator
猜你喜欢

Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go

Pytorch common loss function

CPU负载很低,loadavg很高处理方法

RT-Thread 组件 FinSH 使用时遇到的问题

第五期个人能力认证考核通过名单公布

zabbix 代理服务器 与 zabbix-snmp 监控

反射及在运用过程中出现的IllegalAccessException异常

Don't miss this underestimated movie because of controversy!

全套教学资料,阿里快手拼多多等7家大厂Android面试真题
受益匪浅,安卓面试问题
随机推荐
MySQL information schema learning (I) -- general table
[translation] supply chain security project in toto moved to CNCF incubator
利用 clip-path 绘制不规则的图形
zabbix 代理服务器 与 zabbix-snmp 监控
CF960G - Bandit Blues(第一类斯特林数+OGF)
LeetCode-1279. Traffic light intersection
ModuleNotFoundError: No module named ‘PIL‘解决方法
The second day of rhcsa study
Low CPU load and high loadavg processing method
Detailed idea and code implementation of infix expression to suffix expression
short i =1; i=i+1与short i=1; i+=1的区别
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
深入分析,Android面试真题解析火爆全网
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
测试用里hi
An error occurs when installing MySQL: could not create or access the registry key needed for the
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Interview assault 63: how to remove duplication in MySQL?
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
保证接口数据安全的10种方案