-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add weekly contest 429 (#3877)
- Loading branch information
Showing
18 changed files
with
1,080 additions
and
1 deletion.
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
.../3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
--- | ||
comments: true | ||
difficulty: 简单 | ||
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3396.Minimum%20Number%20of%20Operations%20to%20Make%20Elements%20in%20Array%20Distinct/README.md | ||
--- | ||
|
||
<!-- problem:start --> | ||
|
||
# [3396. 使数组元素互不相同所需的最少操作次数](https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct) | ||
|
||
[English Version](/solution/3300-3399/3396.Minimum%20Number%20of%20Operations%20to%20Make%20Elements%20in%20Array%20Distinct/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- description:start --> | ||
|
||
<p>给你一个整数数组 <code>nums</code>,你需要确保数组中的元素 <strong>互不相同 </strong>。为此,你可以执行以下操作任意次:</p> | ||
|
||
<ul> | ||
<li>从数组的开头移除 3 个元素。如果数组中元素少于 3 个,则移除所有剩余元素。</li> | ||
</ul> | ||
|
||
<p><strong>注意:</strong>空数组也视作为数组元素互不相同。返回使数组元素互不相同所需的 <strong>最少操作次数 </strong>。<!-- notionvc: 210ee4f2-90af-4cdf-8dbc-96d1fa8f67c7 --></p> | ||
|
||
<p> </p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p> | ||
|
||
<p><strong>输出:</strong> <span class="example-io">2</span></p> | ||
|
||
<p><strong>解释:</strong></p> | ||
|
||
<ul> | ||
<li>第一次操作:移除前 3 个元素,数组变为 <code>[4, 2, 3, 3, 5, 7]</code>。</li> | ||
<li>第二次操作:再次移除前 3 个元素,数组变为 <code>[3, 5, 7]</code>,此时数组中的元素互不相同。</li> | ||
</ul> | ||
|
||
<p>因此,答案是 2。</p> | ||
</div> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>输入:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p> | ||
|
||
<p><strong>输出:</strong> <span class="example-io">2</span></p> | ||
|
||
<p><strong>解释:</strong></p> | ||
|
||
<ul> | ||
<li>第一次操作:移除前 3 个元素,数组变为 <code>[4, 4]</code>。</li> | ||
<li>第二次操作:移除所有剩余元素,数组变为空。</li> | ||
</ul> | ||
|
||
<p>因此,答案是 2。</p> | ||
</div> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>输入:</strong> <span class="example-io">nums = [6,7,8,9]</span></p> | ||
|
||
<p><strong>输出:</strong> <span class="example-io">0</span></p> | ||
|
||
<p><strong>解释:</strong></p> | ||
|
||
<p>数组中的元素已经互不相同,因此不需要进行任何操作,答案是 0。</p> | ||
</div> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
</ul> | ||
|
||
<!-- description:end --> | ||
|
||
## 解法 | ||
|
||
<!-- solution:start --> | ||
|
||
### 方法一:哈希表 + 倒序遍历 | ||
|
||
我们可以倒序遍历数组 $\textit{nums}$,并使用哈希表 $\textit{s}$ 记录已经遍历过的元素。当遍历到元素 $\textit{nums}[i]$ 时,如果 $\textit{nums}[i]$ 已经在哈希表 $\textit{s}$ 中,那么说明我们需要移除 $\textit{nums}[0..i]$ 的所有元素,需要的操作次数为 $\left\lfloor \frac{i}{3} \right\rfloor + 1$。否则,我们将 $\textit{nums}[i]$ 加入哈希表 $\textit{s}$ 中,并继续遍历下一个元素。 | ||
|
||
遍历结束后,如果没有找到重复的元素,那么数组中的元素已经互不相同,不需要进行任何操作,答案为 $0$。 | ||
|
||
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 | ||
|
||
<!-- tabs:start --> | ||
|
||
#### Python3 | ||
|
||
```python | ||
class Solution: | ||
def minimumOperations(self, nums: List[int]) -> int: | ||
s = set() | ||
for i in range(len(nums) - 1, -1, -1): | ||
if nums[i] in s: | ||
return i // 3 + 1 | ||
s.add(nums[i]) | ||
return 0 | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
class Solution { | ||
public int minimumOperations(int[] nums) { | ||
Set<Integer> s = new HashSet<>(); | ||
for (int i = nums.length - 1; i >= 0; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.add(nums[i]); | ||
} | ||
return 0; | ||
} | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minimumOperations(vector<int>& nums) { | ||
unordered_set<int> s; | ||
for (int i = nums.size() - 1; ~i; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.insert(nums[i]); | ||
} | ||
return 0; | ||
} | ||
}; | ||
``` | ||
#### Go | ||
```go | ||
func minimumOperations(nums []int) int { | ||
s := map[int]bool{} | ||
for i := len(nums) - 1; i >= 0; i-- { | ||
if s[nums[i]] { | ||
return i/3 + 1 | ||
} | ||
s[nums[i]] = true | ||
} | ||
return 0 | ||
} | ||
``` | ||
|
||
#### TypeScript | ||
|
||
```ts | ||
function minimumOperations(nums: number[]): number { | ||
const s = new Set<number>(); | ||
for (let i = nums.length - 1; ~i; --i) { | ||
if (s.has(nums[i])) { | ||
return Math.ceil((i + 1) / 3); | ||
} | ||
s.add(nums[i]); | ||
} | ||
return 0; | ||
} | ||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- solution:end --> | ||
|
||
<!-- problem:end --> |
178 changes: 178 additions & 0 deletions
178
...96.Minimum Number of Operations to Make Elements in Array Distinct/README_EN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
--- | ||
comments: true | ||
difficulty: Easy | ||
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3396.Minimum%20Number%20of%20Operations%20to%20Make%20Elements%20in%20Array%20Distinct/README_EN.md | ||
--- | ||
|
||
<!-- problem:start --> | ||
|
||
# [3396. Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct) | ||
|
||
[中文文档](/solution/3300-3399/3396.Minimum%20Number%20of%20Operations%20to%20Make%20Elements%20in%20Array%20Distinct/README.md) | ||
|
||
## Description | ||
|
||
<!-- description:start --> | ||
|
||
<p>You are given an integer array <code>nums</code>. You need to ensure that the elements in the array are <strong>distinct</strong>. To achieve this, you can perform the following operation any number of times:</p> | ||
|
||
<ul> | ||
<li>Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.</li> | ||
</ul> | ||
|
||
<p><strong>Note</strong> that an empty array is considered to have distinct elements. Return the <strong>minimum</strong> number of operations needed to make the elements in the array distinct.<!-- notionvc: 210ee4f2-90af-4cdf-8dbc-96d1fa8f67c7 --></p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">2</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<ul> | ||
<li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 2, 3, 3, 5, 7]</code>.</li> | ||
<li>In the second operation, the next 3 elements are removed, resulting in the array <code>[3, 5, 7]</code>, which has distinct elements.</li> | ||
</ul> | ||
|
||
<p>Therefore, the answer is 2.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p> | ||
|
||
<p><strong>Output:</strong> 2</p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<ul> | ||
<li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 4]</code>.</li> | ||
<li>In the second operation, all remaining elements are removed, resulting in an empty array.</li> | ||
</ul> | ||
|
||
<p>Therefore, the answer is 2.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [6,7,8,9]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">0</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>The array already contains distinct elements. Therefore, the answer is 0.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
</ul> | ||
|
||
<!-- description:end --> | ||
|
||
## Solutions | ||
|
||
<!-- solution:start --> | ||
|
||
### Solution 1: Hash Table + Reverse Traversal | ||
|
||
We can traverse the array $\textit{nums}$ in reverse order and use a hash table $\textit{s}$ to record the elements that have already been traversed. When we encounter an element $\textit{nums}[i]$, if $\textit{nums}[i]$ is already in the hash table $\textit{s}$, it means we need to remove all elements from $\textit{nums}[0..i]$. The number of operations required is $\left\lfloor \frac{i}{3} \right\rfloor + 1$. Otherwise, we add $\textit{nums}[i]$ to the hash table $\textit{s}$ and continue to the next element. | ||
|
||
After traversing, if no duplicate elements are found, the elements in the array are already distinct, and no operations are needed, so the answer is $0$. | ||
|
||
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. | ||
|
||
<!-- tabs:start --> | ||
|
||
#### Python3 | ||
|
||
```python | ||
class Solution: | ||
def minimumOperations(self, nums: List[int]) -> int: | ||
s = set() | ||
for i in range(len(nums) - 1, -1, -1): | ||
if nums[i] in s: | ||
return i // 3 + 1 | ||
s.add(nums[i]) | ||
return 0 | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
class Solution { | ||
public int minimumOperations(int[] nums) { | ||
Set<Integer> s = new HashSet<>(); | ||
for (int i = nums.length - 1; i >= 0; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.add(nums[i]); | ||
} | ||
return 0; | ||
} | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minimumOperations(vector<int>& nums) { | ||
unordered_set<int> s; | ||
for (int i = nums.size() - 1; ~i; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.insert(nums[i]); | ||
} | ||
return 0; | ||
} | ||
}; | ||
``` | ||
#### Go | ||
```go | ||
func minimumOperations(nums []int) int { | ||
s := map[int]bool{} | ||
for i := len(nums) - 1; i >= 0; i-- { | ||
if s[nums[i]] { | ||
return i/3 + 1 | ||
} | ||
s[nums[i]] = true | ||
} | ||
return 0 | ||
} | ||
``` | ||
|
||
#### TypeScript | ||
|
||
```ts | ||
function minimumOperations(nums: number[]): number { | ||
const s = new Set<number>(); | ||
for (let i = nums.length - 1; ~i; --i) { | ||
if (s.has(nums[i])) { | ||
return Math.ceil((i + 1) / 3); | ||
} | ||
s.add(nums[i]); | ||
} | ||
return 0; | ||
} | ||
``` | ||
|
||
<!-- tabs:end --> | ||
|
||
<!-- solution:end --> | ||
|
||
<!-- problem:end --> |
13 changes: 13 additions & 0 deletions
13
...00-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution { | ||
public: | ||
int minimumOperations(vector<int>& nums) { | ||
unordered_set<int> s; | ||
for (int i = nums.size() - 1; ~i; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.insert(nums[i]); | ||
} | ||
return 0; | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
...300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
func minimumOperations(nums []int) int { | ||
s := map[int]bool{} | ||
for i := len(nums) - 1; i >= 0; i-- { | ||
if s[nums[i]] { | ||
return i/3 + 1 | ||
} | ||
s[nums[i]] = true | ||
} | ||
return 0 | ||
} |
12 changes: 12 additions & 0 deletions
12
...0-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public int minimumOperations(int[] nums) { | ||
Set<Integer> s = new HashSet<>(); | ||
for (int i = nums.length - 1; i >= 0; --i) { | ||
if (s.contains(nums[i])) { | ||
return i / 3 + 1; | ||
} | ||
s.add(nums[i]); | ||
} | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.