当前位置:网站首页>LeetCode——42. Connected to rainwater (double pointer)
LeetCode——42. Connected to rainwater (double pointer)
2022-06-11 16:50:00 【Always--Learning】
Title Description

Their thinking
Receiving rainwater is a typical double pointer problem , First define a left pointer and a right pointer .
initialization
- The left pointer points to the first element
- The right pointer points to the last element
Define a final return of and sum=0.
Define two variables to store the maximum value on the left and the maximum value on the right .
The loop condition is that the left pointer is smaller than the right pointer .
Each time the loop is entered, the maximum values of the tracks pointed by the left and right pointers are updated .
If the maximum value of the left pointer is less than the maximum value of the right pointer , The rainwater that can be received is the maximum value of the left pointer minus the height of the current position , Then the left pointer moves to the right .
If the maximum value of the left pointer is greater than or equal to the maximum value of the right pointer , Calculate the maximum value of the right pointer minus the current right pointer height , Then move the right pointer to the left .
In order to make it easier for everyone to understand this topic , We can do the same as the following picture , Draw the changes of the left and right pointers and the maximum value in turn , Last return maximum .

AC Code
var trap = function(height) {
let [left, right] = [0, height.length - 1];
let [leftMax, rightMax] = [0, 0];
let sum = 0;
while (left < right) {
leftMax = Math.max(leftMax, height[left]);
rightMax = Math.max(rightMax, height[right]);
if (leftMax < rightMax) {
sum += leftMax - height[left++];
} else {
sum += rightMax - height[right--];
}
}
return sum;
};
reflection
Receiving rain is a topic that is often ridiculed on the pulse , Of course, this question often appears in interview questions such as Niuke , So we must understand this problem , The double pointer question is a kind of question that is often taken in an interview , The most important thing to solve this kind of problem is to figure out when the left pointer and the right pointer move , What is the meaning of the maximum value of the left pointer and the maximum value of the right pointer .
边栏推荐
- (validation file) validatejarfile report errors
- 接口的特点&&接口与抽象类的对比
- Rdkit tutorial
- What products are good for cross-border e-commerce? What are the top selling categories?
- C语言各数据类型的内存映像
- 2022 safety officer-a certificate test question simulation test question bank simulation test platform operation
- R1 Quick Open Pressure Vessel Operation test Library and Simulation Test in 2022
- How to disable the notebook's own keyboard after the notebook is connected to an external keyboard
- Detailed explanation of the functions of list and dict data types
- Go语言之Go 快速入门篇(一):第一个 Go 程序
猜你喜欢

开关电源电路图及原理12v分析-详细版

【clickhouse专栏】新建库、用户及角色

RSP: An Empirical Study of remote sensing pre training

Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)

Research Report on operation mode and investment opportunities of China's aluminum industry 2022-2028

JINTE NET基金会将通过线上直播参与维度链全球战略发布会

ShellBrowser . NET Crack

Development planning of China's stainless steel market and suggestions on the development direction of the 14th five year plan 2022-2028

^32执行上下文栈面试题

核密度估计(二维、三维)
随机推荐
每周推荐短视频:菜鸟CEO谈未来物流新赛道
GemBox. Bundle 43.0 Crack
Message queue push / pull mode Learning & ActiveMQ and JMS learning
Redis - learn five types of NoSQL
Student website template brown cake dessert website design - brown cake dessert shop (4 pages) gourmet dessert website production final assignment finished product_ Fresh fruit web design final assign
Katalon Studio Enterprise
ShellBrowser . NET Crack
项目无法加载nacos配置中心的配置文件问题
启牛商学院给的证券账户是安全的吗?开户收费吗
[Clickhouse column] create a new library, user and role
Rdkit installation
2022高压电工特种作业证考试题库及在线模拟考试
2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试
基于udp端口猜测的内网穿透
Wechat applet timestamp conversion time format + time subtraction
Pychart tips - how to set up a background picture
LeetCode——42. 接雨水(双指针)
ASP.NET教育OA系统源码 教育行业OA系统源码带文档
2022熔化焊接与热切割上岗证题目及模拟考试
Oracle analysis function over and MySQL achieve similar effects