forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-size-of-a-set-after-removals.cpp
50 lines (48 loc) · 1.58 KB
/
maximum-size-of-a-set-after-removals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Time: O(n)
// Space: O(n)
// math, hash table, greedy
class Solution {
public:
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> lookup1(cbegin(nums1), cend(nums1));
unordered_set<int> lookup2(cbegin(nums2), cend(nums2));
unordered_set<int> common;
for (const auto& x : lookup1) {
if (lookup2.count(x)) {
common.emplace(x);
}
}
const int n = size(nums1);
const int n1 = size(lookup1);
const int n2 = size(lookup2);
const int c = size(common);
const int d1 = min(n1 - c, n / 2);
const int d2 = min(n2 - c, n / 2);
return min(n, d1 + d2 + c);
}
};
// Time: O(n)
// Space: O(n)
// math, hash table, greedy
class Solution2 {
public:
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> lookup1(cbegin(nums1), cend(nums1));
unordered_set<int> lookup2(cbegin(nums2), cend(nums2));
unordered_set<int> common;
for (const auto& x : lookup1) {
if (lookup2.count(x)) {
common.emplace(x);
}
}
const int n = size(nums1);
const int n1 = size(lookup1);
const int n2 = size(lookup2);
const int c = size(common);
const int d1 = min(n1 - c, n / 2);
const int d2 = min(n2 - c, n / 2);
const int r1 = n / 2 - d1;
const int r2 = n / 2 - d2;
return d1 + d2 + min(r1 + r2, c); // = min(d1+d2+r1+r2, d1+d2+c) = min(d1+d2+(n//2-d1)+(n//2-d2), d1+d2+c) = min(n, d1+d2+c)
}
};