#2216

Minimum Deletions to Make Array Beautiful

specialist · 880 · lc medium +31 · verified · 49.8% accepted · 835 likes · top 37%

Description

You are given a 0-indexed integer array nums. The array is beautiful if:

- nums.length is even.

- nums[i] != nums[i + 1] for all i where i % 2 == 0.

An empty array is considered beautiful.

You can delete any number of elements. When an element is deleted, elements to its right shift one position left.

Return the minimum number of deletions needed to make nums beautiful.

Example 1:

Input: nums = [1,1,2,3,5]
Output: 1
Explanation: You can delete either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.

Example 2:

Input: nums = [1,1,2,2,3,3]
Output: 2
Explanation: You can delete nums[0] and nums[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.

Code

1
2
3