From c10cf1ed13ab4a2e1d9423da0f80b08ef95f66ec Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 21 Nov 2023 09:13:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=202216?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\250\241\346\213\237.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 110 ++++++++++++++++++ ...10\344\270\255\347\255\211\357\274\211.md" | 4 +- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 "LeetCode/2211-2220/2216. \347\276\216\345\214\226\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 848b6166..eb024355 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -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/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/2211-2220/2216. \347\276\216\345\214\226\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/2211-2220/2216. \347\276\216\345\214\226\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..cfb148c0 --- /dev/null +++ "b/LeetCode/2211-2220/2216. \347\276\216\345\214\226\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\210\240\351\231\244\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -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& 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 原题链接和其他优选题解。 diff --git "a/LeetCode/51-60/53. \346\234\200\345\244\247\345\255\220\346\225\260\347\273\204\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/51-60/53. \346\234\200\345\244\247\345\255\220\346\225\260\347\273\204\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" index 98289753..7f901133 100644 --- "a/LeetCode/51-60/53. \346\234\200\345\244\247\345\255\220\346\225\260\347\273\204\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/51-60/53. \346\234\200\345\244\247\345\255\220\346\225\260\347\273\204\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -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); @@ -193,7 +193,7 @@ C++ 代码: ```C++ class Solution { public: - // 返回值: [sum, max, lm, rm] = [区间和, 最大子数组和, 前缀最大值, 后缀最大值] + // 返回值: [sum, lm, rm, max] = [区间和, 前缀最大值, 后缀最大值, 最大子数组和] vector dfs(vector& nums, int l, int r) { if (l == r) { int t = max(nums[l], 0);