题解 | #接雨水问题#
接雨水问题
http://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
import java.util.*;
public class Solution {
/**
* max water
* @param arr int整型一维数组 the array
* @return long长整型
*/
public long maxWater (int[] arr) {
// write code here
long val = 0;
if(arr.length<=2) return val;
int left = 0;
int right = 1;
while(right<arr.length){
if(arr[left]<=arr[right]){
for(int j = left+1;j<right;j++){
val += arr[left] - arr[j];
}
left = right;
}
right++;
}
if(left==arr.length-1) return val;
right = arr.length-1;
left = arr.length-2;
while(left>=0){
if(arr[right]<=arr[left]){
for(int j = left+1;j<right;j++){
val += arr[right] - arr[j];
}
right = left;
}
left--;
}
return val;
}
}