forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
intersection-of-two-arrays-ii.cpp
124 lines (111 loc) · 3.4 KB
/
intersection-of-two-arrays-ii.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// If the given array is not sorted and the memory is unlimited:
// - Time: O(m + n)
// - Space: O(min(m, n))
// elif the given array is already sorted:
// if m << n or m >> n:
// - Time: O(min(m, n) * log(max(m, n)))
// - Space: O(1)
// else:
// - Time: O(m + n)
// - Soace: O(1)
// else: (the given array is not sorted and the memory is limited)
// - Time: O(max(m, n) * log(max(m, n)))
// - Space: O(1)
// If the given array is not sorted and the memory is unlimited.
// Time: O(m + n)
// Space: O(min(m, n))
// Hash solution.
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size()) {
return intersect(nums2, nums1);
}
unordered_map<int, int> lookup;
for (const auto& i : nums1) {
++lookup[i];
}
vector<int> result;
for (const auto& i : nums2) {
if (lookup[i] > 0) {
result.emplace_back(i);
--lookup[i];
}
}
return result;
}
};
// If the given array is already sorted, and the memory is limited, and (m << n or m >> n).
// Time: O(min(m, n) * log(max(m, n)))
// Space: O(1)
// Binary search solution.
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size()) {
return intersect(nums2, nums1);
}
// Make sure it is sorted, doesn't count in time.
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
vector<int> result;
auto it = nums2.cbegin();
for (const auto& i : nums1) {
it = lower_bound(it, nums2.cend(), i);
if (it != nums2.end() && *it == i) {
result.emplace_back(*it++);
}
}
return result;
}
};
// If the given array is already sorted, and the memory is limited or m ~ n.
// Time: O(m + n)
// Soace: O(1)
// Two pointers solution.
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
// Make sure it is sorted, doesn't count in time.
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
auto it1 = nums1.cbegin(), it2 = nums2.cbegin();
while (it1 != nums1.cend() && it2 != nums2.cend()) {
if (*it1 < *it2) {
++it1;
} else if (*it1 > *it2) {
++it2;
} else {
result.emplace_back(*it1);
++it1, ++it2;
}
}
return result;
}
};
// If the given array is not sorted, and the memory is limited.
// Time: O(max(m, n) * log(max(m, n)))
// Space: O(1)
// Two pointers solution.
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
// O(max(m, n) * log(max(m, n)))
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
auto it1 = nums1.cbegin(), it2 = nums2.cbegin();
while (it1 != nums1.cend() && it2 != nums2.cend()) {
if (*it1 < *it2) {
++it1;
} else if (*it1 > *it2) {
++it2;
} else {
result.emplace_back(*it1);
++it1, ++it2;
}
}
return result;
}
};