Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array nums of integers, a move consists of choosing any element and decreasing it by 1.
An array A is a zigzag array if either:
A[0] > A[1] < A[2] > A[3] < A[4] > ...A[0] < A[1] > A[2] < A[3] > A[4] < ...Return the minimum number of moves to transform the given array nums into a zigzag array.
Example 1:
Input: nums = [1,2,3] Output: 2 Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2] Output: 4
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 1000Problem summary: Given an array nums of integers, a move consists of choosing any element and decreasing it by 1. An array A is a zigzag array if either: Every even-indexed element is greater than adjacent elements, ie. A[0] > A[1] < A[2] > A[3] < A[4] > ... OR, every odd-indexed element is greater than adjacent elements, ie. A[0] < A[1] > A[2] < A[3] > A[4] < ... Return the minimum number of moves to transform the given array nums into a zigzag array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,3]
[9,6,1,6,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1144: Decrease Elements To Make Array Zigzag
class Solution {
public int movesToMakeZigzag(int[] nums) {
int[] ans = new int[2];
int n = nums.length;
for (int i = 0; i < 2; ++i) {
for (int j = i; j < n; j += 2) {
int d = 0;
if (j > 0) {
d = Math.max(d, nums[j] - nums[j - 1] + 1);
}
if (j < n - 1) {
d = Math.max(d, nums[j] - nums[j + 1] + 1);
}
ans[i] += d;
}
}
return Math.min(ans[0], ans[1]);
}
}
// Accepted solution for LeetCode #1144: Decrease Elements To Make Array Zigzag
func movesToMakeZigzag(nums []int) int {
ans := [2]int{}
n := len(nums)
for i := 0; i < 2; i++ {
for j := i; j < n; j += 2 {
d := 0
if j > 0 {
d = max(d, nums[j]-nums[j-1]+1)
}
if j < n-1 {
d = max(d, nums[j]-nums[j+1]+1)
}
ans[i] += d
}
}
return min(ans[0], ans[1])
}
# Accepted solution for LeetCode #1144: Decrease Elements To Make Array Zigzag
class Solution:
def movesToMakeZigzag(self, nums: List[int]) -> int:
ans = [0, 0]
n = len(nums)
for i in range(2):
for j in range(i, n, 2):
d = 0
if j:
d = max(d, nums[j] - nums[j - 1] + 1)
if j < n - 1:
d = max(d, nums[j] - nums[j + 1] + 1)
ans[i] += d
return min(ans)
// Accepted solution for LeetCode #1144: Decrease Elements To Make Array Zigzag
struct Solution;
impl Solution {
fn moves_to_make_zigzag(nums: Vec<i32>) -> i32 {
let mut sums = vec![0, 0];
let n = nums.len();
for i in 0..n {
let mut adj = vec![];
if i > 0 {
adj.push(nums[i - 1]);
}
if i + 1 < n {
adj.push(nums[i + 1]);
}
let min = adj.into_iter().min().unwrap();
if nums[i] >= min {
sums[i % 2] += (nums[i] - min) + 1;
}
}
sums[0].min(sums[1])
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3];
let res = 2;
assert_eq!(Solution::moves_to_make_zigzag(nums), res);
let nums = vec![9, 6, 1, 6, 2];
let res = 4;
assert_eq!(Solution::moves_to_make_zigzag(nums), res);
let nums = vec![10, 4, 4, 10, 10, 6, 2, 3];
let res = 13;
assert_eq!(Solution::moves_to_make_zigzag(nums), res);
}
// Accepted solution for LeetCode #1144: Decrease Elements To Make Array Zigzag
function movesToMakeZigzag(nums: number[]): number {
const ans: number[] = Array(2).fill(0);
const n = nums.length;
for (let i = 0; i < 2; ++i) {
for (let j = i; j < n; j += 2) {
let d = 0;
if (j > 0) {
d = Math.max(d, nums[j] - nums[j - 1] + 1);
}
if (j < n - 1) {
d = Math.max(d, nums[j] - nums[j + 1] + 1);
}
ans[i] += d;
}
}
return Math.min(...ans);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.