Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. /*
  2. https://leetcode.com/problems/trapping-rain-water/
  3. Runtime: 1 ms, faster than 98.11% of Java online submissions for Trapping Rain Water.
  4. Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Trapping Rain Water.
  5. */
  6. class Solution {
  7.     public int trap(int[] height) {
  8.         int waters = 0, l = 0, r = height.length - 1;
  9.         while (l < r) {
  10.             int lh = height[l], rh = height[r];
  11.             if (lh < rh) {
  12.                 while(++l < r && lh >= height[l]) {
  13.                     waters += lh - height[l];
  14.                 }
  15.             } else {
  16.                 while(l < --r && rh >= height[r]) {
  17.                     waters += rh - height[r];
  18.                 }
  19.             }
  20.         }
  21.         return waters;
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement