2024-04-12 Daily Challenge
Today I have done leetcode's April LeetCoding Challenge with cpp
.
April LeetCoding Challenge 12
Description
Trapping Rain Water
Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5] Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
int trap(vector<int>& height) {
vector<int> maxRights{0};
int len = height.size();
for(int i = len - 1; i >= 0; --i) {
if(height[i] >= maxRights.back()) {
maxRights.push_back(height[i]);
}
}
int maxLeft = 0;
int answer = 0;
for(auto h : height) {
maxLeft = max(maxLeft, h);
int maxRight = maxRights.back();
int waterHeight = maxLeft > maxRight ? maxRight : maxLeft;
answer += waterHeight > h ? (waterHeight - h) : 0;
if(h == maxRight) {
maxRights.pop_back();
}
}
return answer;
}
};
// Accepted
// 322/322 cases passed (21 ms)
// Your runtime beats 72.47 % of cpp submissions
// Your memory usage beats 43.13 % of cpp submissions (20.2 MB)