3396. Minimum Number of Operations to Make Elements in Array Distinct Easy

@problem@discussion
#Array#Hash Table



1/**
2 * [3396] Minimum Number of Operations to Make Elements in Array Distinct
3 *
4 * You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:
5 * 
6 * 	Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
7 * 
8 * Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.<!-- notionvc: 210ee4f2-90af-4cdf-8dbc-96d1fa8f67c7 -->
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span>
13 * Output: <span class="example-io">2</span>
14 * Explanation:
15 * 
16 * 	In the first operation, the first 3 elements are removed, resulting in the array [4, 2, 3, 3, 5, 7].
17 * 	In the second operation, the next 3 elements are removed, resulting in the array [3, 5, 7], which has distinct elements.
18 * 
19 * Therefore, the answer is 2.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [4,5,6,4,4]</span>
24 * Output: 2
25 * Explanation:
26 * 
27 * 	In the first operation, the first 3 elements are removed, resulting in the array [4, 4].
28 * 	In the second operation, all remaining elements are removed, resulting in an empty array.
29 * 
30 * Therefore, the answer is 2.
31 * </div>
32 * <strong class="example">Example 3:
33 * <div class="example-block">
34 * Input: <span class="example-io">nums = [6,7,8,9]</span>
35 * Output: <span class="example-io">0</span>
36 * Explanation:
37 * The array already contains distinct elements. Therefore, the answer is 0.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	1 <= nums.length <= 100
43 * 	1 <= nums[i] <= 100
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/
49// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn minimum_operations(nums: Vec<i32>) -> i32 {
55        0
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_3396() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.