Код: Выделить всё
public static int minOperations(List nums) {
return Math.min(zigzagCount(nums, 0), zigzagCount(nums, 1));
}
// pattern: 0 for even-index valleys, 1 for odd-index valleys
private static int zigzagCount(List nums, int pattern) {
int n = nums.size();
int count = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == pattern) {
int left = (i > 0) ? nums.get(i - 1) : Integer.MAX_VALUE;
int right = (i + 1 < n) ? nums.get(i + 1) : Integer.MAX_VALUE;
if (nums.get(i) >= Math.min(left, right)) {
count++;
}
}
}
return count;
}
Input: [2, 1, 2, 3, 4, 5, 2, 9], Minimum Replacements: 3
expected: 2
Input: [8, 2, 1, 2, 3, 4, 5, 2, 9], Минимальные замены: 3
Ожидается: 2
Подробнее здесь: https://stackoverflow.com/questions/796 ... is-passing