Advertisement
nikunjsoni

42

Mar 31st, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int trap(vector<int>& height) {
  4.         int left, right, ans, left_max, right_max;
  5.         left = 0; right = height.size()-1;
  6.         left_max = right_max = ans = 0;
  7.        
  8.         while(left < right){
  9.             if(height[left] < height[right]){
  10.                 height[left] >= left_max ? left_max = height[left] : ans += (left_max - height[left]);
  11.                 left++;
  12.             }
  13.             else{
  14.                 height[right] >= right_max ? right_max = height[right] : ans += (right_max - height[right]);
  15.                 right--;
  16.             }
  17.         }
  18.         return ans;
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement