当前位置:网站首页>363 · 接雨水
363 · 接雨水
2022-04-22 06:15:00 【yinhua405】
描述
给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。

微信加 jiuzhang15 发送验证信息【国内大厂】领字节、阿里、百度等最新高频题
样例
样例 1:
输入: [0,1,0]
输出: 0
样例 2:
输入: [0,1,0,2,1,0,1,3,2,1,2,1]
输出: 6
挑战
O(n) 时间, O(1) 空间
O(n) 时间, O(n) 空间也可以接受
int trapRainWater(vector<int> &heights)
{
if (heights.size() < 2)
{
return 0;
}
int sum = 0;
int leftCurMax = 0;
vector<int> rightMaxVec(heights.size(),-1);
int left = 0;
leftCurMax = max(leftCurMax, heights[left]);
int rightMax = 0;
for (int right = heights.size() - 1; right >= 2; right--)
{
rightMaxVec[right] = max(rightMax, heights[right]);
rightMax = rightMaxVec[right];
}
sum += max(0, min(leftCurMax, rightMax) - heights[1]);
for (int index = 2; index < heights.size()-1; index++)
{
leftCurMax = max(leftCurMax, heights[index-1]);
int rightMax = rightMaxVec[index+1];
sum += max(0,min(leftCurMax, rightMax) - heights[index]);
}
return sum;
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/123470194
边栏推荐
- 1242 · 无重叠区间
- (一)Sql Server的下载与安装
- Pycharm only needs five steps to improve the download speed by using Tsinghua image source
- 双向循环链表(详)
- secureCRT无限循环脚本
- [number theory] congruence (2): inverse element
- Host cannot Ping virtual machine in bridging mode
- Suppose there is such a relationship between the weight and height of adults: height (CM) - 100 = standard weight (kg). If the difference between a person's weight and its standard weight is between p
- [number theory] congruence (V): multivariate linear congruence equation
- 【数论】素数(四):数的分解(Pollard-rho)
猜你喜欢
随机推荐
[number theory] congruence (V): multivariate linear congruence equation
[number theory] prime number (2): prime number sieve method (Egyptian sieve, Euler sieve, interval sieve)
Host cannot Ping virtual machine in bridging mode
What is the internal structure of stack frame?
Points for attention in Modelsim simulation acceleration
Written examination for summer internship of meituan spring recruitment -- 20220312
Pycharm only needs five steps to improve the download speed by using Tsinghua image source
虚拟机磁盘空间缩小
C语言 | 预处理
[DRC 23-20] Rule violation (REQP-1712) Input clock driver - Unsupported PLLE2_ADV connectivity.
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
If an error is reported, process the ES6 filter to filter the array
838 · 子数组和为K
Quartus II prevents signals from being integrated
384 · 最长无重复字符的子串
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
链表习题详解
(2) Basic configuration of SQL server and connection to SQL server using Navicat
最强操作符学习之路(C语言详解)
(6) DCL and DML syntax of SQL Server









