Skip to content

Latest commit

 

History

History
182 lines (151 loc) · 4.11 KB

File metadata and controls

182 lines (151 loc) · 4.11 KB

中文文档

Description

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 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.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105

Solutions

Python3

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
        if (n - ans) % 2:
            ans += 1
        return ans

Java

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;
            }
        }
        if ((n - ans) % 2 == 1) {
            ++ans;
        }
        return ans;
    }
}

TypeScript

function minDeletion(nums: number[]): number {
    const n = nums.length;
    let res = 0;
    let i = 0;
    while (i < n - 1) {
        if (nums[i] === nums[i + 1]) {
            i++;
            res++;
        } else {
            i += 2;
        }
    }
    if ((n - res) % 2 === 1) {
        res++;
    }
    return res;
}

Rust

impl Solution {
    pub fn min_deletion(nums: Vec<i32>) -> i32 {
        let n = nums.len();
        let mut res = 0;
        let mut i = 0;
        while i < n - 1 {
            if nums[i] == nums[i + 1] {
                res += 1;
                i += 1;
            } else {
                i += 2;
            }
        }
        if (n - res) % 2 == 1 {
            res += 1;
        }
        res as i32
    }
}

C++

class Solution {
public:
    int minDeletion(vector<int>& nums) {
        int n = nums.size();
        int ans = 0;
        for (int i = 0; i < n - 1; ++i) {
            if (nums[i] == nums[i + 1]) {
                ++ans;
            } else {
                ++i;
            }
        }
        if ((n - ans) % 2) ++ans;
        return ans;
    }
};

Go

func minDeletion(nums []int) int {
	n := len(nums)
	ans := 0
	for i := 0; i < n-1; i++ {
		if nums[i] == nums[i+1] {
			ans++
		} else {
			i++
		}
	}
	if (n-ans)%2 == 1 {
		ans++
	}
	return ans
}

...