import java.util.*;
public class Solution {
/**
* max water
* @param arr int整型一维数组 the array
* @return long长整型
*/
public long maxWater (int[] arr) {
// write code here
int len = arr.length;
int left = 0;
int right = len - 1;
if(len < 3)
return 0;
int count = 0;
int maxL = 0;
int maxR = 0;
while(left < right){
maxL = Math.max(arr[left], maxL);
maxR = Math.max(arr[right], maxR);
if(maxR > maxL){
count += maxL - arr[left];
left++;
}else{
count += maxR - arr[right];
right--;
}
}
return count;
}
}
牛客-TOP101-BM94
最新推荐文章于 2024-11-05 21:58:11 发布