SHARE
TWEET

Untitled

ogv Sep 18th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     public int trap(int[] height) {
  3.         if (height.length == 0) return 0;
  4.        
  5.         int totalWater = 0;
  6.         int currentWater = 0;
  7.        
  8.         int max = height[0];
  9.         for (int h: height) {
  10.             if (h <= max) {
  11.                 currentWater += (max - h);
  12.             }
  13.             else {
  14.                 max = h;
  15.                 totalWater += currentWater;
  16.             }
  17.         }
  18.        
  19.         return totalWater;
  20.     }
  21. }
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