LeetCode 1144. Decrease Elements To Make Array Zigzag

Problem Statement


impl Solution {
pub fn moves_to_make_zigzag(nums: Vec<i32>) -> i32 {
use std::cmp;
let mut moves = vec![0, 0];
let (mut l, mut r) = (0, 0);
for i in 0..nums.len() {
if i == 0 {
l = std::i32::MAX;
} else {
l = nums[i - 1];
}
if i == nums.len() - 1 {
r = std::i32::MAX;
} else {
r = nums[i + 1];
}

moves[i % 2] += cmp::max(0, nums[i] - cmp::min(l, r) + 1);
}
return std::cmp::min(moves[0], moves[1]);
}
}