当前位置:网站首页>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
边栏推荐
- map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
- Tensorflow2.0 自定义训练的方式求解函数系数
- Interface test tool - postman
- 主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
- CPU负载很低,loadavg很高处理方法
- English topic assignment (25)
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
- The second day of rhcsa study
- PMP每日一练 | 考试不迷路-7.6
猜你喜欢
ACTF 2022圆满落幕,0ops战队二连冠!!
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
业务与应用同步发展:应用现代化的策略建议
Dark horse -- redis
Interface test tool - postman
冒烟测试怎么做
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
Benefit a lot, Android interview questions
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
随机推荐
LeetCode-1279. 红绿灯路口
Zero foundation entry polardb-x: build a highly available system and link the big data screen
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
Interface test tool - postman
In 50W, what have I done right?
[translation] supply chain security project in toto moved to CNCF incubator
Translation D28 (with AC code POJ 26:the nearest number)
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
Mysql Information Schema 学习(一)--通用表
short i =1; i=i+1与short i=1; i+=1的区别
黑马--Redis篇
Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
Take a look at how cabloyjs workflow engine implements activiti boundary events
时钟轮在 RPC 中的应用
测试用里hi
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
力扣101题:对称二叉树
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!