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[] temp = new int[n]; Arrays.fill(temp, 1); for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { temp[i] = temp[i - 1] + 1; } } int res = temp[n - 1]; for (int i = n - 2; i >= 0; i--) { if (arr[i] > arr[i + 1] && temp[i] <= temp[i + 1]) { temp[i] = temp[i + 1] + 1; } res += temp[i]; } return res; } }