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 is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
The above elevation map 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.Thanks Marcos for contributing this image!
int trap(int A[], int n)
{
int left[n];
int right[n];
int leftMax = 0;
for (int i = 0; i < n; i++)
{
left[i] = leftMax;
leftMax = max(leftMax, A[i]);
}
int rightMax = 0;
for (int i = n-1; i >= 0; i--)
{
right[i] = rightMax;
rightMax = max(rightMax, A[i]);
}
int result = 0;
for (int i = 0; i < n; i++)
{
int temp = min(left[i], right[i]) - A[i];
if (temp > 0)
{
result += temp;
}
}
return result;
}
相關文章
- Trapping-rain-waterAPPAI
- Leetcode 42 Trapping Rain WaterLeetCodeAPPAI
- Leetcode Trapping Raining waterLeetCodeAPPAI
- LeetCode 42. Trapping Rain WaterLeetCodeAPPAI
- Leetcode 之 PHP 解析 (42. Trapping Rain Water)LeetCodePHPAPPAI
- C. Mixing Water
- LeetCode-Water and Jug ProblemLeetCode
- 11. Container With Most WaterAI
- 水紋效果(Water)源程式. (轉)
- leetcode Container With Most WaterLeetCodeAI
- constraint=constraints?AI
- [LeetCode] 417. Pacific Atlantic Water FlowLeetCode
- Leetcode 11 Container With Most WaterLeetCodeAI
- Leetcode-Container With Most WaterLeetCodeAI
- Container With Most Water leetcode javaAILeetCodeJava
- leetcode_11. Container With Most WaterLeetCodeAI
- LeetCode 11. Container With Most WaterLeetCodeAI
- Kiss the rainAI
- trainAI
- TrainerAI
- Codeforces Round 689(Div.2) E題 Water Level
- LeetCode - Medium - 11. Container With Most WaterLeetCodeAI
- Oracle段高水位(HWM, high water mark)問題Oracle
- 沙因的睡蓮模型(Water lily model) (轉載)模型
- Android ConstraintLayout 2.0:ConstraintLayoutStatesAndroidAI
- Flutter | ConstrainedBox & UnconstrainedBox 元件FlutterAI元件
- ConstraintAI
- rhythm of the rainAI
- sabsatrainingAI
- Brainpan(VulnHub)AI
- Flutter深入淺出元件篇---ConstrainedBox、BoxConstraints、UnconstrainedBoxFlutter元件AI
- Water well drilling mud cleaning and mixing unit to MongoliaGo
- Heibei solids control Water Based Mud (WBM) Recovery SystemSolid
- [LeetCode] Container With Most Water 裝最多水的容器LeetCodeAI
- 8、orainstRoot.sh和 oraInst.locAI
- Fine Grained DependenciesAI
- biti_rainyAI
- refrain from caffeineAI