|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +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 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [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) |
| 10 | + |
| 11 | +[中文文档](/solution/3300-3399/3396.Minimum%20Number%20of%20Operations%20to%20Make%20Elements%20in%20Array%20Distinct/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<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> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<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> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p> |
| 30 | + |
| 31 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 32 | + |
| 33 | +<p><strong>Explanation:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 2, 3, 3, 5, 7]</code>.</li> |
| 37 | + <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> |
| 38 | +</ul> |
| 39 | + |
| 40 | +<p>Therefore, the answer is 2.</p> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">Example 2:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> 2</p> |
| 49 | + |
| 50 | +<p><strong>Explanation:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 4]</code>.</li> |
| 54 | + <li>In the second operation, all remaining elements are removed, resulting in an empty array.</li> |
| 55 | +</ul> |
| 56 | + |
| 57 | +<p>Therefore, the answer is 2.</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p><strong class="example">Example 3:</strong></p> |
| 61 | + |
| 62 | +<div class="example-block"> |
| 63 | +<p><strong>Input:</strong> <span class="example-io">nums = [6,7,8,9]</span></p> |
| 64 | + |
| 65 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 66 | + |
| 67 | +<p><strong>Explanation:</strong></p> |
| 68 | + |
| 69 | +<p>The array already contains distinct elements. Therefore, the answer is 0.</p> |
| 70 | +</div> |
| 71 | + |
| 72 | +<p> </p> |
| 73 | +<p><strong>Constraints:</strong></p> |
| 74 | + |
| 75 | +<ul> |
| 76 | + <li><code>1 <= nums.length <= 100</code></li> |
| 77 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 78 | +</ul> |
| 79 | + |
| 80 | +<!-- description:end --> |
| 81 | + |
| 82 | +## Solutions |
| 83 | + |
| 84 | +<!-- solution:start --> |
| 85 | + |
| 86 | +### Solution 1: Hash Table + Reverse Traversal |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +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$. |
| 91 | + |
| 92 | +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. |
| 93 | + |
| 94 | +<!-- tabs:start --> |
| 95 | + |
| 96 | +#### Python3 |
| 97 | + |
| 98 | +```python |
| 99 | +class Solution: |
| 100 | + def minimumOperations(self, nums: List[int]) -> int: |
| 101 | + s = set() |
| 102 | + for i in range(len(nums) - 1, -1, -1): |
| 103 | + if nums[i] in s: |
| 104 | + return i // 3 + 1 |
| 105 | + s.add(nums[i]) |
| 106 | + return 0 |
| 107 | +``` |
| 108 | + |
| 109 | +#### Java |
| 110 | + |
| 111 | +```java |
| 112 | +class Solution { |
| 113 | + public int minimumOperations(int[] nums) { |
| 114 | + Set<Integer> s = new HashSet<>(); |
| 115 | + for (int i = nums.length - 1; i >= 0; --i) { |
| 116 | + if (s.contains(nums[i])) { |
| 117 | + return i / 3 + 1; |
| 118 | + } |
| 119 | + s.add(nums[i]); |
| 120 | + } |
| 121 | + return 0; |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +#### C++ |
| 127 | + |
| 128 | +```cpp |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + int minimumOperations(vector<int>& nums) { |
| 132 | + unordered_set<int> s; |
| 133 | + for (int i = nums.size() - 1; ~i; --i) { |
| 134 | + if (s.contains(nums[i])) { |
| 135 | + return i / 3 + 1; |
| 136 | + } |
| 137 | + s.insert(nums[i]); |
| 138 | + } |
| 139 | + return 0; |
| 140 | + } |
| 141 | +}; |
| 142 | +``` |
| 143 | +
|
| 144 | +#### Go |
| 145 | +
|
| 146 | +```go |
| 147 | +func minimumOperations(nums []int) int { |
| 148 | + s := map[int]bool{} |
| 149 | + for i := len(nums) - 1; i >= 0; i-- { |
| 150 | + if s[nums[i]] { |
| 151 | + return i/3 + 1 |
| 152 | + } |
| 153 | + s[nums[i]] = true |
| 154 | + } |
| 155 | + return 0 |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +#### TypeScript |
| 160 | + |
| 161 | +```ts |
| 162 | +function minimumOperations(nums: number[]): number { |
| 163 | + const s = new Set<number>(); |
| 164 | + for (let i = nums.length - 1; ~i; --i) { |
| 165 | + if (s.has(nums[i])) { |
| 166 | + return Math.ceil((i + 1) / 3); |
| 167 | + } |
| 168 | + s.add(nums[i]); |
| 169 | + } |
| 170 | + return 0; |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +<!-- tabs:end --> |
| 175 | + |
| 176 | +<!-- solution:end --> |
| 177 | + |
| 178 | +<!-- problem:end --> |
0 commit comments