题解 | #接雨水问题#
接雨水问题
https://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * max water * @param arr int整型一维数组 the array * @return long长整型 */ public long maxWater (List<int> arr) { int left = 0, right = arr.Count - 1; long res = 0; int i = -1; while(left < right){ if(arr[left] <= arr[right]){ for(i = left + 1; i < right; i++){ if(arr[i] > arr[left]){ left = i; break; } res += arr[left] - arr[i]; //Console.WriteLine("left++"+res); } if(i == right) break; } else{ for(i = right - 1; i > left; i--){ if(arr[i] > arr[right]){ right = i; break; } res += arr[right] - arr[i]; //Console.WriteLine("right--"+res); } if(i == left) break; } } return res; } }