You are viewing a single comment's thread. Return to all comments →
My solution in Java 8:
public static long candies(int n, List<Integer> arr) { int[] result = new int[n]; Arrays.fill(result, 1); // crescator for (int i = 1; i < n; i++) { if (arr.get(i) > arr.get(i - 1)) { result[i] = result[i - 1] + 1; } } // descrescator for (int i = n - 1; i > 0; i--) { if (arr.get(i) < arr.get(i - 1) && (result[i - 1] <= result[i])) { result[i - 1] = result[i] + 1; } } long candies = 0; for (int i = 0; i < n; i++) { candies += result[i]; } return candies; }
Candies
You are viewing a single comment's thread. Return to all comments →
My solution in Java 8: