Advertisement
ogv

Untitled

ogv
Sep 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement