返回介绍

solution / 2200-2299 / 2216.Minimum Deletions to Make Array Beautiful / README_EN

发布于 2024-06-17 01:03:08 字数 7195 浏览 0 评论 0 收藏 0

2216. Minimum Deletions to Make Array Beautiful

中文文档

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

Solution 1: Greedy

According to the problem description, we know that a beautiful array has an even number of elements, and if we divide every two adjacent elements in this array into a group, then the two elements in each group are not equal. This means that the elements within a group cannot be repeated, but the elements between groups can be repeated.

Therefore, we consider traversing the array from left to right. As long as we encounter two adjacent elements that are equal, we delete one of them, that is, the deletion count increases by one; otherwise, we can keep these two elements.

Finally, we check whether the length of the array after deletion is even. If not, it means that we need to delete one more element to make the final array length even.

The time complexity is $O(n)$, where $n$ is the length of the array. We only need to traverse the array once. The space complexity is $O(1)$.

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

Solution 2

class Solution:
  def minDeletion(self, nums: List[int]) -> int:
    n = len(nums)
    ans = i = 0
    while i < n:
      j = i + 1
      while j < n and nums[j] == nums[i]:
        j += 1
        ans += 1
      i = j + 1
    ans += (n - ans) % 2
    return ans
class Solution {
  public int minDeletion(int[] nums) {
    int n = nums.length;
    int ans = 0;
    for (int i = 0; i < n;) {
      int j = i + 1;
      while (j < n && nums[j] == nums[i]) {
        ++j;
        ++ans;
      }
      i = j + 1;
    }
    ans += (n - ans) % 2;
    return ans;
  }
}
class Solution {
public:
  int minDeletion(vector<int>& nums) {
    int n = nums.size();
    int ans = 0;
    for (int i = 0; i < n;) {
      int j = i + 1;
      while (j < n && nums[j] == nums[i]) {
        ++j;
        ++ans;
      }
      i = j + 1;
    }
    ans += (n - ans) % 2;
    return ans;
  }
};
func minDeletion(nums []int) (ans int) {
  n := len(nums)
  for i := 0; i < n; {
    j := i + 1
    for ; j < n && nums[j] == nums[i]; j++ {
      ans++
    }
    i = j + 1
  }
  ans += (n - ans) % 2
  return
}
function minDeletion(nums: number[]): number {
  const n = nums.length;
  let ans = 0;
  for (let i = 0; i < n; ) {
    let j = i + 1;
    for (; j < n && nums[j] === nums[i]; ++j) {
      ++ans;
    }
    i = j + 1;
  }
  ans += (n - ans) % 2;
  return ans;
}
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 {
      let mut j = i + 1;
      while j < n && nums[j] == nums[i] {
        ans += 1;
        j += 1;
      }
      i = j + 1;
    }
    ans += (n - ans) % 2;
    ans as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文