Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int trap(int[] height) {
- if (height.length == 0) return 0;
- int totalWater = 0;
- int currentWater = 0;
- int max = height[0];
- for (int h: height) {
- if (h <= max) {
- currentWater += (max - h);
- }
- else {
- max = h;
- totalWater += currentWater;
- }
- }
- return totalWater;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement