import java.util.*; public class Solution { /** * pick candy * @param arr int整型一维数组 the array * @return int整型 */ public int candy (int[] arr) { // write code here if (arr == null || arr.length < 1) { return 0; } int n = arr.length; if (n == 1) { return 1; } int[] tmp = new int[n]; Arrays.fill(tmp, 1); for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { tmp[i] = tmp[i - 1] + 1; } } int res = tmp[n - 1]; for (int i = n - 2; i >= 0; i--) { if (arr[i] > arr[i + 1] && tmp[i] <= tmp[i + 1]) { tmp[i] = tmp[i + 1] + 1; } res += tmp[i]; } return res; } }