当前位置:网站首页>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
边栏推荐
- Swagger2 reports an error illegal DefaultValue null for parameter type integer
- The slave i/o thread stops because master and slave have equal MySQL serv
- Computer network: sorting out common network interview questions (I)
- An error occurs when installing MySQL: could not create or access the registry key needed for the
- Druid 数据库连接池 详解
- 潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
- 深入分析,Android面试真题解析火爆全网
- Mysql Information Schema 学习(一)--通用表
- Documents to be used in IC design process
- 反射及在运用过程中出现的IllegalAccessException异常
猜你喜欢
Leetcode 30. 串联所有单词的子串
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Pytorch common loss function
An error occurs when installing MySQL: could not create or access the registry key needed for the
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
打家劫舍III[后序遍历与回溯+动态规划]
Druid database connection pool details
保证接口数据安全的10种方案
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
随机推荐
Take a look at how cabloyjs workflow engine implements activiti boundary events
A method of removing text blur based on pixel repair
LeetCode-1279. Traffic light intersection
数学知识——高斯消元(初等行变换解方程组)代码实现
从sparse.csc.csr_matrix生成邻接矩阵
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
The second day of rhcsa study
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Test technology stack arrangement -- self cultivation of test development engineers
Translation D28 (with AC code POJ 26:the nearest number)
测试用里hi
Interview assault 63: how to remove duplication in MySQL?
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
JDBC details
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
tensorflow和torch代码验证cuda是否安装成功
C language daily practice - day 22: Zero foundation learning dynamic planning