当前位置:网站首页>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 .
边栏推荐
- 所见即所得的 markdown 编辑器:Typora
- ^31原型面试题
- Pychart tips - how to set up a background picture
- Association relationship
- Memory image of various data types in C language
- A team of heavyweights came to the "digital transformation" arena of CLP Jinxin ice and snow sports
- Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
- 2022 safety officer-a certificate test question simulation test question bank simulation test platform operation
- Rdkit tutorial
- 信息收集常用工具及命令
猜你喜欢

LeetCode——42. 接雨水(双指针)

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

2022起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试

学生网站模板棕色蛋糕甜品网站设计——棕色蛋糕甜品店(4页) 美食甜品网页制作期末大作业成品_生鲜水果网页设计期末作业

RSP:遥感预训练的实证研究

2022年R1快開門式壓力容器操作考試題庫及模擬考試

leetcode684. Redundant connection (medium)

2022 molten welding and thermal cutting work license and simulation examination

485 days, 21 experiences of my remote office sharing | community essay solicitation

网络流媒体协议的联系与区别(RTP RTCP RTSP RTMP HLS)
随机推荐
2022年安全员-B证国家题库及模拟考试
[ISITDTU 2019]EasyPHP
unittest 如何知道每个测试用例的执行时间
2022安全员-C证特种作业证考试题库及答案
Detailed explanation of the functions of list and dict data types
Elasitcsearch基础学习笔记(1)
7个人生工具:SWOT、PDCA、6W2H、SMART、WBS、时间管理、二八原则
How to store tree structure in database
Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
GemBox. Bundle 43.0 Crack
select into from 和 insert into select 区别
Leetcode 450. Deleting a node in a binary search tree
【pytest学习】pytest 用例执行失败后其他不再执行
2022 national question bank and mock examination for safety officer-b certificate
RSP: An Empirical Study of remote sensing pre training
Oracle database merge row records, wmsys WM_ Use of the concat function and group in MySQL_ Use and comparison of concat (ID).
Analysis report on the "fourteenth five year plan" proposal and innovation environment of global and Chinese sodium pyrophosphate industry (2022-2028)
Analysis report on future development trend and investment suggestions of global and Chinese soybean protein industry 2022-2028
每周推荐短视频:菜鸟CEO谈未来物流新赛道
Web security - shooting range notes