Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int trap(vector<int>& height) {
  4. int left = 0, right = height.size() - 1, ans = 0;
  5.  
  6. while(left < right) {
  7. int minH = min(height[left], height[right]);
  8.  
  9. //> start from minH to find how much water can be trapped
  10. if(height[left] == minH) {
  11. ++left;
  12. while(left < right && height[left] < minH) {
  13. ans += minH - height[left];
  14. ++left;
  15. }
  16. }
  17. else {
  18. --right;
  19. while(left < right && height[right] < minH) {
  20. ans += minH - height[right];
  21. --right;
  22. }
  23. }
  24. }
  25.  
  26. return ans;
  27. }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement