Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int trap(vector<int>& height) {
- int left, right, ans, left_max, right_max;
- left = 0; right = height.size()-1;
- left_max = right_max = ans = 0;
- while(left < right){
- if(height[left] < height[right]){
- height[left] >= left_max ? left_max = height[left] : ans += (left_max - height[left]);
- left++;
- }
- else{
- height[right] >= right_max ? right_max = height[right] : ans += (right_max - height[right]);
- right--;
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement