Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int trap(int[] height) {
- int ans = 0;
- int left = 0;
- int right = height.length-1;
- int leftMax = 0;
- int rightMax = 0;
- while(left < right) {
- if(height[left] < height[right]) {
- if(height[left] >= leftMax) {
- leftMax = height[left];
- } else {
- ans += leftMax - height[left];
- }
- left += 1;
- } else {
- if(height[right] >= rightMax) {
- rightMax = height[right];
- } else {
- ans += rightMax - height[right];
- }
- right -= 1;
- }
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement