Skip to content

Commit 95ba8c4

Browse files
committed
feat: add weekly contest 481 & biweekly contest 172
1 parent 1186fef commit 95ba8c4

File tree

58 files changed

+3225
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3225
-4
lines changed

solution/2000-2099/2045.Second Minimum Time to Reach Destination/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ The red path shows the path to get the second minimum time.
6161
- 3 -> 4: 3 minutes, time elapsed=6
6262
- Wait at 4 for 4 minutes, time elapsed=10
6363
- 4 -> 5: 3 minutes, time elapsed=13
64-
Hence the second minimum time is 13 minutes.
65-
</pre>
64+
Hence the second minimum time is 13 minutes.
65+
</pre>
6666

6767
<p><strong class="example">Example 2:</strong></p>
6868
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2045.Second%20Minimum%20Time%20to%20Reach%20Destination/images/eg2.png" style="width: 225px; height: 50px;" />

solution/3700-3799/3774.Absolute Difference Between Maximum and Minimum K Elements/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3774.Absolute%20Difference%20Between%20Maximum%20and%20Minimum%20K%20Elements/README.md
5+
rating: 1206
6+
source: 第 480 场周赛 Q1
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3774.Absolute Difference Between Maximum and Minimum K Elements/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3774.Absolute%20Difference%20Between%20Maximum%20and%20Minimum%20K%20Elements/README_EN.md
5+
rating: 1206
6+
source: Weekly Contest 480 Q1
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3775.Reverse Words With Same Vowel Count/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3775.Reverse%20Words%20With%20Same%20Vowel%20Count/README.md
5+
rating: 1391
6+
source: 第 480 场周赛 Q2
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3775.Reverse Words With Same Vowel Count/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3775.Reverse%20Words%20With%20Same%20Vowel%20Count/README_EN.md
5+
rating: 1391
6+
source: Weekly Contest 480 Q2
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3776.Minimum Moves to Balance Circular Array/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3776.Minimum%20Moves%20to%20Balance%20Circular%20Array/README.md
5+
rating: 1739
6+
source: 第 480 场周赛 Q3
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3776.Minimum Moves to Balance Circular Array/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3776.Minimum%20Moves%20to%20Balance%20Circular%20Array/README_EN.md
5+
rating: 1739
6+
source: Weekly Contest 480 Q3
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3777.Minimum Deletions to Make Alternating Substring/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3777.Minimum%20Deletions%20to%20Make%20Alternating%20Substring/README.md
5+
rating: 2201
6+
source: 第 480 场周赛 Q4
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3777.Minimum Deletions to Make Alternating Substring/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3777.Minimum%20Deletions%20to%20Make%20Alternating%20Substring/README_EN.md
5+
rating: 2201
6+
source: Weekly Contest 480 Q4
57
---
68

79
<!-- problem:start -->
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3779.Minimum%20Number%20of%20Operations%20to%20Have%20Distinct%20Elements/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3779. 得到互不相同元素的最少操作次数](https://leetcode.cn/problems/minimum-number-of-operations-to-have-distinct-elements)
10+
11+
[English Version](/solution/3700-3799/3779.Minimum%20Number%20of%20Operations%20to%20Have%20Distinct%20Elements/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code>。</p>
18+
19+
<p>在一次操作中,你需要移除当前数组的 <strong>前三个元素</strong>。如果剩余元素少于三个,则移除 <strong>所有</strong> 剩余元素。</p>
20+
21+
<p>重复此操作,直到数组为空或不包含任何重复元素为止。</p>
22+
23+
<p>返回一个整数,表示所需的操作次数。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">nums = [3,8,3,6,5,8]</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p>在第一次操作中,我们移除前三个元素。剩余的元素 <code>[6, 5, 8]</code> 互不相同,因此停止。仅需要一次操作。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>输入:</strong> <span class="example-io">nums = [2,2]</span></p>
43+
44+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<p>经过一次操作后,数组变为空,满足停止条件。</p>
49+
</div>
50+
51+
<p><strong class="example">示例 3:</strong></p>
52+
53+
<div class="example-block">
54+
<p><strong>输入:</strong> <span class="example-io">nums = [4,3,5,1,2]</span></p>
55+
56+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
57+
58+
<p><strong>解释:</strong></p>
59+
60+
<p>数组中的所有元素都是互不相同的,因此不需要任何操作。</p>
61+
</div>
62+
63+
<p>&nbsp;</p>
64+
65+
<p><strong>提示:</strong></p>
66+
67+
<ul>
68+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
69+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
70+
</ul>
71+
72+
<!-- description:end -->
73+
74+
## 解法
75+
76+
<!-- solution:start -->
77+
78+
### 方法一:哈希表 + 倒序遍历
79+
80+
我们可以倒序遍历数组 $\textit{nums}$,并使用哈希表 $\textit{st}$ 记录已经遍历过的元素。当遍历到元素 $\textit{nums}[i]$ 时,如果 $\textit{nums}[i]$ 已经在哈希表 $\textit{st}$ 中,那么说明我们需要移除 $\textit{nums}[0..i]$ 的所有元素,需要的操作次数为 $\left\lfloor \frac{i}{3} \right\rfloor + 1$。否则,我们将 $\textit{nums}[i]$ 加入哈希表 $\textit{st}$ 中,并继续遍历下一个元素。
81+
82+
遍历结束后,如果没有找到重复的元素,那么数组中的元素已经互不相同,不需要进行任何操作,答案为 $0$。
83+
84+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
85+
86+
<!-- tabs:start -->
87+
88+
#### Python3
89+
90+
```python
91+
class Solution:
92+
def minOperations(self, nums: List[int]) -> int:
93+
st = set()
94+
for i in range(len(nums) - 1, -1, -1):
95+
if nums[i] in st:
96+
return i // 3 + 1
97+
st.add(nums[i])
98+
return 0
99+
```
100+
101+
#### Java
102+
103+
```java
104+
class Solution {
105+
public int minOperations(int[] nums) {
106+
Set<Integer> st = new HashSet<>();
107+
for (int i = nums.length - 1; i >= 0; --i) {
108+
if (!st.add(nums[i])) {
109+
return i / 3 + 1;
110+
}
111+
}
112+
return 0;
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int minOperations(vector<int>& nums) {
123+
unordered_set<int> st;
124+
for (int i = nums.size() - 1; ~i; --i) {
125+
if (st.contains(nums[i])) {
126+
return i / 3 + 1;
127+
}
128+
st.insert(nums[i]);
129+
}
130+
return 0;
131+
}
132+
};
133+
```
134+
135+
#### Go
136+
137+
```go
138+
func minOperations(nums []int) int {
139+
st := make(map[int]struct{})
140+
for i := len(nums) - 1; i >= 0; i-- {
141+
if _, ok := st[nums[i]]; ok {
142+
return i/3 + 1
143+
}
144+
st[nums[i]] = struct{}{}
145+
}
146+
return 0
147+
}
148+
```
149+
150+
#### TypeScript
151+
152+
```ts
153+
function minOperations(nums: number[]): number {
154+
const st = new Set<number>();
155+
for (let i = nums.length - 1; i >= 0; i--) {
156+
if (st.has(nums[i])) {
157+
return Math.floor(i / 3) + 1;
158+
}
159+
st.add(nums[i]);
160+
}
161+
return 0;
162+
}
163+
```
164+
165+
<!-- tabs:end -->
166+
167+
<!-- solution:end -->
168+
169+
<!-- problem:end -->

0 commit comments

Comments
 (0)