Skip to content

Commit ceb2ee5

Browse files
Update optimized approach code
1 parent ac030a6 commit ceb2ee5

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
Intuition
2-
Using hash map to opimize the algorithm o(N). Instead of using sorting. It takes O(NlogN). But my solution takes require more space then sorting approach.
2+
We have to check whether the current element in the current array is also present in the other array that has been given to us, What data structure can we use here?
3+
34
Approach
4-
My first thought is using a "Hash_map" to store the elements of 1 array and corresponding frequency into hash map.
5-
Add the elements of "nums1" into map with corresponding frequency.
6-
Iterate throught each element of "nums2". If frequency of the element stored in hash map greater than 0. We add this element into "res" and decrease the frequency of this element.
5+
We simply store one array in a map with all its frequencies, and while iterating the other given array if we encounter the current element of the current array in the map with suitable frequency we will add that element to our answer vector. Also remember that we have to delete any element that has 0 frequncy to avoid adding extra elements to our answer vector.
6+
77
Complexity
8-
Time complexity: O(N).
9-
Space complexity: O(N).
8+
Time complexity:
9+
O(n+m) Iterating over the vectors and finding the element in tree map.
10+
11+
Space complexity:
12+
O(n) An answer vector to return and a map to store elements.
Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
#include <iostream>
22
#include <vector>
33
#include <unordered_map>
4-
#include <algorithm>
54
using namespace std;
6-
7-
class Solution {
5+
#define nline '\n'
6+
#define sp ' '
7+
#define all(x) x.begin(), x.end()
8+
#define rall(x) x.rbegin(), x.rend()
9+
static int fastIO = []
10+
{
11+
ios_base::sync_with_stdio(false);
12+
cin.tie(0);
13+
cout.tie(0);
14+
return 0;
15+
}();
16+
// Paste the Class here-
17+
class Solution
18+
{
819
public:
9-
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
10-
vector<int> res;
11-
unordered_map<int, int> box;
12-
for (int num : nums1)
13-
box[num]++;
14-
for (int i = 0; i < nums2.size(); ++i) {
15-
if (box[nums2[i]] > 0) {
16-
res.push_back(nums2[i]);
17-
box[nums2[i]]--;
18-
} else {
19-
continue;
20+
vector<int> intersect(vector<int> &nums1, vector<int> &nums2)
21+
{
22+
unordered_map<int, int> hashmap;
23+
vector<int> Res;
24+
for (int i = 0; i < nums1.size(); i++)
25+
{
26+
hashmap[nums1[i]]++;
27+
}
28+
for (int i = 0; i < nums2.size(); i++)
29+
{
30+
int element = nums2[i];
31+
if (hashmap.find(element) != hashmap.end())
32+
{
33+
Res.push_back(element);
34+
hashmap[element]--;
35+
if (hashmap[element] == 0)
36+
{
37+
hashmap.erase(element);
38+
}
2039
}
2140
}
22-
return res;
41+
return Res;
2342
}
2443
};

0 commit comments

Comments
 (0)