SHARE
TWEET

Untitled

a guest Sep 12th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. https://leetcode.com/problems/container-with-most-water/
  3. Runtime: 2 ms, faster than 94.52% of Java online submissions for Container With Most Water.
  4. Memory Usage: 39.5 MB, less than 96.15% of Java online submissions for Container With Most Water.
  5. */
  6. class Solution {
  7.     public int maxArea(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.             waters = Math.max(waters, (r-l) * Math.min(lh, rh));
  12.             if (lh < rh) {
  13.                 ++l;
  14.             } else {
  15.                 --r;
  16.             }
  17.         }
  18.         return waters;
  19.     }
  20. }
  21.  
  22. /*
  23. https://leetcode.com/problems/trapping-rain-water/
  24. Runtime: 1 ms, faster than 98.12% of Java online submissions for Trapping Rain Water.
  25. Memory Usage: 37.7 MB, less than 95.89% of Java online submissions for Trapping Rain Water.
  26. */
  27. class Solution {
  28.     public int trap(int[] height) {
  29.         int waters = 0, l = 0, r = height.length - 1;
  30.         while (l < r) {
  31.             int lh = height[l], rh = height[r];
  32.             if (lh < rh) {
  33.                 while(++l < r && lh >= height[l]) {
  34.                     waters += lh - height[l];
  35.                 }
  36.             } else {
  37.                 while(l < --r && rh >= height[r]) {
  38.                     waters += rh - height[r];
  39.                 }
  40.             }
  41.         }
  42.         return waters;
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top