Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3f1b602

Browse files
committed
Add solution #3002
1 parent d8248c3 commit 3f1b602

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,7 @@
21252125
2997|[Minimum Number of Operations to Make Array XOR Equal to K](./solutions/2997-minimum-number-of-operations-to-make-array-xor-equal-to-k.js)|Medium|
21262126
2998|[Minimum Number of Operations to Make X and Y Equal](./solutions/2998-minimum-number-of-operations-to-make-x-and-y-equal.js)|Medium|
21272127
2999|[Count the Number of Powerful Integers](./solutions/2999-count-the-number-of-powerful-integers.js)|Hard|
2128+
3002|[Maximum Size of a Set After Removals](./solutions/3002-maximum-size-of-a-set-after-removals.js)|Medium|
21282129
3024|[Type of Triangle](./solutions/3024-type-of-triangle.js)|Easy|
21292130
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21302131
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 3002. Maximum Size of a Set After Removals
3+
* https://leetcode.com/problems/maximum-size-of-a-set-after-removals/
4+
* Difficulty: Medium
5+
*
6+
* You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
7+
*
8+
* You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals,
9+
* you insert the remaining elements of nums1 and nums2 into a set s.
10+
*
11+
* Return the maximum possible size of the set s.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums1
16+
* @param {number[]} nums2
17+
* @return {number}
18+
*/
19+
var maximumSetSize = function(nums1, nums2) {
20+
const n = nums1.length;
21+
const set1 = new Set(nums1);
22+
const set2 = new Set(nums2);
23+
24+
let unique1 = set1.size;
25+
let unique2 = set2.size;
26+
let common = 0;
27+
28+
for (const num of set1) {
29+
if (set2.has(num)) common++;
30+
}
31+
32+
unique1 -= common;
33+
unique2 -= common;
34+
35+
const max1 = Math.min(unique1, n / 2);
36+
const max2 = Math.min(unique2, n / 2);
37+
const remaining = n / 2 - max1 + n / 2 - max2;
38+
const maxCommon = Math.min(common, remaining);
39+
40+
return max1 + max2 + maxCommon;
41+
};

0 commit comments

Comments
 (0)