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.
You are given a 0-indexed integer array nums. The array nums is beautiful if:
nums.length is even.nums[i] != nums[i + 1] for all i % 2 == 0.Note that an empty array is considered beautiful.
You can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.
Return the minimum number of elements to delete from nums to make it beautiful.
Example 1:
Input: nums = [1,1,2,3,5] Output: 1 Explanation: You can delete eithernums[0]ornums[1]to makenums= [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to makenumsbeautiful.
Example 2:
Input: nums = [1,1,2,2,3,3] Output: 2 Explanation: You can deletenums[0]andnums[5]to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem summary: You are given a 0-indexed integer array nums. The array nums is beautiful if: nums.length is even. nums[i] != nums[i + 1] for all i % 2 == 0. Note that an empty array is considered beautiful. You can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged. Return the minimum number of elements to delete from nums to make it beautiful.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack · Greedy
[1,1,2,3,5]
[1,1,2,2,3,3]
minimum-deletions-to-make-character-frequencies-unique)minimum-operations-to-make-the-array-alternating)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2216: Minimum Deletions to Make Array Beautiful
class Solution {
public int minDeletion(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n - 1; ++i) {
if (nums[i] == nums[i + 1]) {
++ans;
} else {
++i;
}
}
ans += (n - ans) % 2;
return ans;
}
}
// Accepted solution for LeetCode #2216: Minimum Deletions to Make Array Beautiful
func minDeletion(nums []int) (ans int) {
n := len(nums)
for i := 0; i < n-1; i++ {
if nums[i] == nums[i+1] {
ans++
} else {
i++
}
}
ans += (n - ans) % 2
return
}
# Accepted solution for LeetCode #2216: Minimum Deletions to Make Array Beautiful
class Solution:
def minDeletion(self, nums: List[int]) -> int:
n = len(nums)
i = ans = 0
while i < n - 1:
if nums[i] == nums[i + 1]:
ans += 1
i += 1
else:
i += 2
ans += (n - ans) % 2
return ans
// Accepted solution for LeetCode #2216: Minimum Deletions to Make Array Beautiful
impl Solution {
pub fn min_deletion(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut ans = 0;
let mut i = 0;
while i < n - 1 {
if nums[i] == nums[i + 1] {
ans += 1;
i += 1;
} else {
i += 2;
}
}
ans += (n - ans) % 2;
ans as i32
}
}
// Accepted solution for LeetCode #2216: Minimum Deletions to Make Array Beautiful
function minDeletion(nums: number[]): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n - 1; ++i) {
if (nums[i] === nums[i + 1]) {
++ans;
} else {
++i;
}
}
ans += (n - ans) % 2;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
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: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.
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.