Skip to content

Commit 2d76a3f

Browse files
committed
Sync LeetCode submission Runtime - 7 ms (100.00%), Memory - 17.6 MB (100.00%)
1 parent 37a1447 commit 2d76a3f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<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>
2+
3+
<ul>
4+
<li>Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.</li>
5+
</ul>
6+
7+
<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>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<ul>
20+
<li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 2, 3, 3, 5, 7]</code>.</li>
21+
<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>
22+
</ul>
23+
24+
<p>Therefore, the answer is 2.</p>
25+
</div>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p>
31+
32+
<p><strong>Output:</strong> 2</p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<ul>
37+
<li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 4]</code>.</li>
38+
<li>In the second operation, all remaining elements are removed, resulting in an empty array.</li>
39+
</ul>
40+
41+
<p>Therefore, the answer is 2.</p>
42+
</div>
43+
44+
<p><strong class="example">Example 3:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>Input:</strong> <span class="example-io">nums = [6,7,8,9]</span></p>
48+
49+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
50+
51+
<p><strong>Explanation:</strong></p>
52+
53+
<p>The array already contains distinct elements. Therefore, the answer is 0.</p>
54+
</div>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
61+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
62+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
4+
class Solution:
5+
def minimumOperations(self, nums: List[int]) -> int:
6+
count = 0
7+
8+
while len(set(nums)) != len(nums):
9+
nums = nums[3: ]
10+
count += 1
11+
12+
return count
13+

0 commit comments

Comments
 (0)