3002. Maximum Size of a Set After Removals Medium

@problem@discussion
#Array#Hash Table#Greedy



1/**
2 * [3002] Maximum Size of a Set After Removals
3 *
4 * You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
5 * You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
6 * Return the maximum possible size of the set s.
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
11 * Output: 2
12 * Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
13 * It can be shown that 2 is the maximum possible size of the set s after the removals.
14 * 
15 * <strong class="example">Example 2:
16 * 
17 * Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
18 * Output: 5
19 * Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
20 * It can be shown that 5 is the maximum possible size of the set s after the removals.
21 * 
22 * <strong class="example">Example 3:
23 * 
24 * Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
25 * Output: 6
26 * Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
27 * It can be shown that 6 is the maximum possible size of the set s after the removals.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	n == nums1.length == nums2.length
33 * 	1 <= n <= 2 * 10^4
34 * 	n is even.
35 * 	1 <= nums1[i], nums2[i] <= 10^9
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/maximum-size-of-a-set-after-removals/
41// discuss: https://leetcode.com/problems/maximum-size-of-a-set-after-removals/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn maximum_set_size(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_3002() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.