Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added intersectionOfTwoArraysII
  • Loading branch information
Vally79 committed May 22, 2016
commit 96f799f843841463d31f6fea6f82be7aed2b9771
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp)|Easy|
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [C++](./algorithms/cpp/intersectionOfTwoArrays/intersectionOfTwoArrays.cpp)|Easy|
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium|
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/
// Author : Calinescu Valentin
// Date : 2016-05-22

/***************************************************************************************
*
* Given two arrays, write a function to compute their intersection.
*
* Example:
* Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
*
* Note:
* Each element in the result should appear as many times as it shows in both arrays.
* The result can be in any order.
*
* Follow up:
* What if the given array is already sorted? How would you optimize your algorithm?
* What if nums1's size is small compared to num2's size? Which algorithm is better?
* What if elements of nums2 are stored on disk, and the memory is limited such that you
* cannot load all elements into the memory at once?
*
***************************************************************************************/

/* Solution
* --------
*
* Follow up:
*
* 1)If the given array is already sorted we can skip the sorting.
*
* 2)If nums1 is significantly smaller than nums2 we can only sort nums1 and then binary
* search every element of nums2 in nums1 with a total complexity of (MlogN) or if nums2
* is already sorted we can search every element of nums1 in nums2 in O(NlogM)
*
* 3)Just like 2), we can search for every element in nums2, thus having an online
* algorithm.
*/

class Solution { // O(NlogN + MlogM)
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());//we sort both vectors in order to intersect
sort(nums2.begin(), nums2.end());//them later in O(N + M), where N = nums1.size()
vector <int> solution; //M = nums2.size()
int index = 0;
bool finished = false;
for(int i = 0; i < nums1.size() && !finished; i++)
{
while(index < nums2.size() && nums1[i] > nums2[index])//we skip over the
index++;//smaller elements in nums2
if(index == nums2.size())//we have reached the end of nums2 so we have no more
finished = true;//elements to add to the intersection
else if(nums1[i] == nums2[index])//we found a common element
{
solution.push_back(nums1[i]);
index++;
}
}
return solution;
}
};