3779. Minimum Number of Operations to Have Distinct Elements Medium
1/**
2 * [3779] Minimum Number of Operations to Have Distinct Elements
3 *
4 * You are given an integer array nums.
5 * In one operation, you remove the first three elements of the current array. If there are fewer than three elements remaining, all remaining elements are removed.
6 * Repeat this operation until the array is empty or contains no duplicate values.
7 * Return an integer denoting the number of operations required.
8 *
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [3,8,3,6,5,8]</span>
12 * Output: <span class="example-io">1</span>
13 * Explanation:
14 * In the first operation, we remove the first three elements. The remaining elements [6, 5, 8] are all distinct, so we stop. Only one operation is needed.
15 * </div>
16 * <strong class="example">Example 2:
17 * <div class="example-block">
18 * Input: <span class="example-io">nums = [2,2]</span>
19 * Output: <span class="example-io">1</span>
20 * Explanation:
21 * After one operation, the array becomes empty, which meets the stopping condition.
22 * </div>
23 * <strong class="example">Example 3:
24 * <div class="example-block">
25 * Input: <span class="example-io">nums = [4,3,5,1,2]</span>
26 * Output: <span class="example-io">0</span>
27 * Explanation:
28 * All elements in the array are distinct, therefore no operations are needed.
29 * </div>
30 *
31 * Constraints:
32 *
33 * 1 <= nums.length <= 10^5
34 * 1 <= nums[i] <= 10^5
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/minimum-number-of-operations-to-have-distinct-elements/
40// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-have-distinct-elements/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn min_operations(nums: Vec<i32>) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_3779() {
58 }
59}
60Back
© 2026 bowen.ge All Rights Reserved.