Skip to content

✨feat: add 2216 #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
| [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 |
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
| [2103. 环和杆](https://leetcode.cn/problems/rings-and-rods/) | [LeetCode 题解链接](https://leetcode.cn/problems/rings-and-rods/solutions/2509056/gong-shui-san-xie-liang-ge-jiao-du-jin-x-r1v1/) | 简单 | 🤩🤩🤩🤩 |
| [2216. 美化数组的最少删除数](https://leetcode.cn/problems/minimum-deletions-to-make-array-beautiful/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-deletions-to-make-array-beautiful/solutions/2535327/gong-shui-san-xie-zhi-ji-ben-zhi-de-ji-j-dk05/) | 中等 | 🤩🤩🤩🤩 |
| [2335. 装满杯子需要的最短总时长](https://leetcode.cn/problems/minimum-amount-of-time-to-fill-cups/) | [LeetCode 题解链接](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247495870&idx=1&sn=a15b87852faaa33fc9d976b575ef1099) | 简单 | 🤩🤩🤩🤩🤩 |
| [2342. 数位和相等数对的最大和](https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/solutions/2531511/gong-shui-san-xie-yong-bian-li-guo-cheng-kt3f/) | 中等 | 🤩🤩🤩🤩 |
| [2520. 统计能整除数字的位数](https://leetcode.cn/problems/count-the-digits-that-divide-a-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-the-digits-that-divide-a-number/solutions/2498966/gong-shui-san-xie-jian-dan-mo-ni-ti-shi-0ad2c/) | 简单 | 🤩🤩🤩🤩🤩 |
Expand Down
110 changes: 110 additions & 0 deletions LeetCode/2211-2220/2216. 美化数组的最少删除数(中等).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
### 题目描述

这是 LeetCode 上的 **[2216. 美化数组的最少删除数](https://leetcode.cn/problems/minimum-deletions-to-make-array-beautiful/solutions/2535327/gong-shui-san-xie-zhi-ji-ben-zhi-de-ji-j-dk05/)** ,难度为 **中等**。

Tag : 「模拟」



给你一个下标从 `0` 开始的整数数组 `nums`,如果满足下述条件,则认为数组 `nums` 是一个 美丽数组 :

* `nums.length` 为偶数
* 对所有满足 `i % 2 == 0` 的下标 `i`,`nums[i] != nums[i + 1]` 均成立

注意,空数组同样认为是美丽数组。

你可以从 `nums` 中删除任意数量的元素。当你删除一个元素时,被删除元素右侧的所有元素将会向左移动一个单位以填补空缺,而左侧的元素将会保持 不变 。

返回使 `nums` 变为美丽数组所需删除的最少元素数目。

示例 1:
```
输入:nums = [1,1,2,3,5]

输出:1

解释:可以删除 nums[0] 或 nums[1] ,这样得到的 nums = [1,2,3,5] 是一个美丽数组。可以证明,要想使 nums 变为美丽数组,至少需要删除 1 个元素。
```
示例 2:
```
输入:nums = [1,1,2,2,3,3]

输出:2

解释:可以删除 nums[0] 和 nums[5] ,这样得到的 nums = [1,2,2,3] 是一个美丽数组。可以证明,要想使 nums 变为美丽数组,至少需要删除 2 个元素。
```

提示:
* $1 <= nums.length <= 10^5$
* $0 <= nums[i] <= 10^5$

---

### 模拟

使用变量 `cnt` 代表已删除的元素个数,由于每次删除元素,剩余元素都会往前移动,因此当前下标为 $i - cnt$。

处理 `nums` 过程中,若当前下标为偶数,且与下一位置元素相同,那么当前元素需被删除,令 `cnt` 自增。

最终数组长度为 $n - cnt$,若长度为奇数,需要再额外删除结尾元素(`cnt` 再加一),否则 `cnt` 即为答案。

Java 代码:

```Java
class Solution {
public int minDeletion(int[] nums) {
int n = nums.length, cnt = 0;
for (int i = 0; i < n; i++) {
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
}
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
}
}
```
C++ 代码:
```C++
class Solution {
public:
int minDeletion(vector<int>& nums) {
int n = nums.size(), cnt = 0;
for (int i = 0; i < n; i++) {
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
}
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
}
};
```
Python 代码:
```Python
class Solution:
def minDeletion(self, nums: List[int]) -> int:
n, cnt = len(nums), 0
for i in range(n):
if (i - cnt) % 2 == 0 and i + 1 < n and nums[i] == nums[i + 1]:
cnt += 1
return cnt + 1 if (n - cnt) % 2 != 0 else cnt
```
TypeScript 代码:
```TypeScript
function minDeletion(nums: number[]): number {
let n = nums.length, cnt = 0;
for (let i = 0; i < n; i++) {
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
}
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
};
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(1)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.2216` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
4 changes: 2 additions & 2 deletions LeetCode/51-60/53. 最大子数组和(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Java 代码:

```Java
class Solution {
// 返回值: [sum, max, lm, rm] = [区间和, 最大子数组和, 前缀最大值, 后缀最大值]
// 返回值: [sum, lm, rm, max] = [区间和, 前缀最大值, 后缀最大值, 最大子数组和]
int[] dfs(int[] nums, int l, int r) {
if (l == r) {
int t = Math.max(nums[l], 0);
Expand Down Expand Up @@ -193,7 +193,7 @@ C++ 代码:
```C++
class Solution {
public:
// 返回值: [sum, max, lm, rm] = [区间和, 最大子数组和, 前缀最大值, 后缀最大值]
// 返回值: [sum, lm, rm, max] = [区间和, 前缀最大值, 后缀最大值, 最大子数组和]
vector<int> dfs(vector<int>& nums, int l, int r) {
if (l == r) {
int t = max(nums[l], 0);
Expand Down