2870. Minimum Number of Operations to Make Array Empty Medium

@problem@discussion
#Array#Hash Table#Greedy#Counting



1/**
2 * [2870] Minimum Number of Operations to Make Array Empty
3 *
4 * You are given a 0-indexed array nums consisting of positive integers.
5 * There are two types of operations that you can apply on the array any number of times:
6 * 
7 * 	Choose two elements with equal values and delete them from the array.
8 * 	Choose three elements with equal values and delete them from the array.
9 * 
10 * Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
11 *  
12 * <strong class="example">Example 1:
13 * 
14 * Input: nums = [2,3,3,2,2,4,2,3,4]
15 * Output: 4
16 * Explanation: We can apply the following operations to make the array empty:
17 * - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
18 * - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
19 * - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
20 * - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
21 * It can be shown that we cannot make the array empty in less than 4 operations.
22 * 
23 * <strong class="example">Example 2:
24 * 
25 * Input: nums = [2,1,2,2,3,3]
26 * Output: -1
27 * Explanation: It is impossible to empty the array.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	2 <= nums.length <= 10^5
33 * 	1 <= nums[i] <= 10^6
34 * 
35 *  
36 * Note: This question is the same as <a href="https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/" target="_blank">2244: Minimum Rounds to Complete All Tasks.</a>
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/
42// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn min_operations(nums: Vec<i32>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2870() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.