trapping-rain-water 1.0.0
Trapping Rain Water
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

int trap (vector< int > &height)
 

Detailed Description

Definition at line 6 of file main.cpp.

Member Function Documentation

◆ trap()

int Solution::trap ( vector< int > &  height)
inline

Definition at line 9 of file main.cpp.

10 {
11 int total_volume = 0;
12 int left = 0; // left index
13 int right = height.size() - 1; // right index
14 int left_max = 0; // max height searching from left
15 int right_max = 0; // max height searching from right
16
17 // scan: left -->> <<-- right
18 while (left <= right)
19 {
20 if (height[left] <= height[right])
21 {
22 if (height[left] >= left_max) // update maximum height on the left
23 left_max = height[left];
24 else // add water volume
25 total_volume += left_max - height[left];
26 left++;
27 }
28 else
29 {
30 if (height[right] >= right_max) // update maximum height on the right
31 right_max = height[right];
32 else // add water volume
33 total_volume += right_max - height[right];
34 right--;
35 }
36 }
37 return total_volume;
38 }

Referenced by main().


The documentation for this class was generated from the following file: