# leetcode 42. Trapping Rain Water Use first for loop to find highest bar and its id. Then trace from left and right to this id individually. ```cpp class Solution { public: int trap(vector<int>& height) { int max = -1, max_id, cur_hei = 0, res = 0, size = height.size(); for(int i = 0; i < size; i++){ if(height[i] > max){ max = height[i]; max_id = i; } } for(int i = 0; i < max_id; i++){ if(height[i] > cur_hei) cur_hei = height[i]; else res += cur_hei - height[i]; } cur_hei = 0; for(int i = size - 1; i > max_id; i--){ if(height[i] > cur_hei) cur_hei = height[i]; else res += cur_hei - height[i]; } return res; } }; ``` ###### tags: `leetcode` `c++`