题解 | #接雨水问题#
接雨水问题
http://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
/** * max water * @param arr int整型一维数组 the array * @param arrLen int arr数组长度 * @return long长整型 */ long long maxWater(int* arr, int arrLen) { // write code here int i = 0; int tmp = 0; int j = 0; long long num = 0; int pos = -1; while (i<arrLen-1) { j = i + 1; for ( ; j<arrLen; j++) { if (arr[j]>tmp) { tmp = arr[j]; pos = j; } if (arr[j] >= arr[i]) { for (int n = i; n<j; n++) { num = num + arr[i] - arr[n]; tmp = 0; } i = j; break; } } if (j >= arrLen) { j = i + 1; for (; j<pos; j++) { num = num + tmp - arr[j]; } i = pos; tmp = 0; } } return num; }