Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c10cf1e

Browse files
✨feat: add 2216
1 parent e8d9f73 commit c10cf1e

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
| [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/) | 简单 | 🤩🤩🤩🤩 |
246246
| [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/) | 中等 | 🤩🤩🤩🤩 |
247247
| [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/) | 简单 | 🤩🤩🤩🤩 |
248+
| [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/) | 中等 | 🤩🤩🤩🤩 |
248249
| [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) | 简单 | 🤩🤩🤩🤩🤩 |
249250
| [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/) | 中等 | 🤩🤩🤩🤩 |
250251
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
### 题目描述
2+
3+
这是 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/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个下标从 `0` 开始的整数数组 `nums`,如果满足下述条件,则认为数组 `nums` 是一个 美丽数组 :
10+
11+
* `nums.length` 为偶数
12+
* 对所有满足 `i % 2 == 0` 的下标 `i`,`nums[i] != nums[i + 1]` 均成立
13+
14+
注意,空数组同样认为是美丽数组。
15+
16+
你可以从 `nums` 中删除任意数量的元素。当你删除一个元素时,被删除元素右侧的所有元素将会向左移动一个单位以填补空缺,而左侧的元素将会保持 不变 。
17+
18+
返回使 `nums` 变为美丽数组所需删除的最少元素数目。
19+
20+
示例 1:
21+
```
22+
输入:nums = [1,1,2,3,5]
23+
24+
输出:1
25+
26+
解释:可以删除 nums[0] 或 nums[1] ,这样得到的 nums = [1,2,3,5] 是一个美丽数组。可以证明,要想使 nums 变为美丽数组,至少需要删除 1 个元素。
27+
```
28+
示例 2:
29+
```
30+
输入:nums = [1,1,2,2,3,3]
31+
32+
输出:2
33+
34+
解释:可以删除 nums[0] 和 nums[5] ,这样得到的 nums = [1,2,2,3] 是一个美丽数组。可以证明,要想使 nums 变为美丽数组,至少需要删除 2 个元素。
35+
```
36+
37+
提示:
38+
* 1ドル <= nums.length <= 10^5$
39+
* 0ドル <= nums[i] <= 10^5$
40+
41+
---
42+
43+
### 模拟
44+
45+
使用变量 `cnt` 代表已删除的元素个数,由于每次删除元素,剩余元素都会往前移动,因此当前下标为 $i - cnt$。
46+
47+
处理 `nums` 过程中,若当前下标为偶数,且与下一位置元素相同,那么当前元素需被删除,令 `cnt` 自增。
48+
49+
最终数组长度为 $n - cnt,ドル若长度为奇数,需要再额外删除结尾元素(`cnt` 再加一),否则 `cnt` 即为答案。
50+
51+
Java 代码:
52+
53+
```Java
54+
class Solution {
55+
public int minDeletion(int[] nums) {
56+
int n = nums.length, cnt = 0;
57+
for (int i = 0; i < n; i++) {
58+
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
59+
}
60+
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
61+
}
62+
}
63+
```
64+
C++ 代码:
65+
```C++
66+
class Solution {
67+
public:
68+
int minDeletion(vector<int>& nums) {
69+
int n = nums.size(), cnt = 0;
70+
for (int i = 0; i < n; i++) {
71+
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
72+
}
73+
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
74+
}
75+
};
76+
```
77+
Python 代码:
78+
```Python
79+
class Solution:
80+
def minDeletion(self, nums: List[int]) -> int:
81+
n, cnt = len(nums), 0
82+
for i in range(n):
83+
if (i - cnt) % 2 == 0 and i + 1 < n and nums[i] == nums[i + 1]:
84+
cnt += 1
85+
return cnt + 1 if (n - cnt) % 2 != 0 else cnt
86+
```
87+
TypeScript 代码:
88+
```TypeScript
89+
function minDeletion(nums: number[]): number {
90+
let n = nums.length, cnt = 0;
91+
for (let i = 0; i < n; i++) {
92+
if ((i - cnt) % 2 == 0 && i + 1 < n && nums[i] == nums[i + 1]) cnt++;
93+
}
94+
return (n - cnt) % 2 != 0 ? cnt + 1 : cnt;
95+
};
96+
```
97+
* 时间复杂度:$O(n)$
98+
* 空间复杂度:$O(1)$
99+
100+
---
101+
102+
### 最后
103+
104+
这是我们「刷穿 LeetCode」系列文章的第 `No.2216` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
105+
106+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
107+
108+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
109+
110+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

‎LeetCode/51-60/53. 最大子数组和(中等).md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Java 代码:
164164

165165
```Java
166166
class Solution {
167-
// 返回值: [sum, max, lm, rm] = [区间和, 最大子数组和, 前缀最大值, 后缀最大值]
167+
// 返回值: [sum, lm, rm, max] = [区间和, 前缀最大值, 后缀最大值, 最大子数组和]
168168
int[] dfs(int[] nums, int l, int r) {
169169
if (l == r) {
170170
int t = Math.max(nums[l], 0);
@@ -193,7 +193,7 @@ C++ 代码:
193193
```C++
194194
class Solution {
195195
public:
196-
// 返回值: [sum, max, lm, rm] = [区间和, 最大子数组和, 前缀最大值, 后缀最大值]
196+
// 返回值: [sum, lm, rm, max] = [区间和, 前缀最大值, 后缀最大值, 最大子数组和]
197197
vector<int> dfs(vector<int>& nums, int l, int r) {
198198
if (l == r) {
199199
int t = max(nums[l], 0);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /