leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 694f0d79713b44a71674bb2a7f1c59fa474cf6dc
parent 57fb1fbf59c96003141dd02b1ba506be1c0f196e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 10 Mar 2024 20:10:05 +0000

Daily Problem

Diffstat:
AProblems/0349.cpp | 38++++++++++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/Problems/0349.cpp b/Problems/0349.cpp @@ -0,0 +1,38 @@ + +class Solution { + public: + vector<int> intersection(vector<int> &nums1, vector<int> &nums2) const { + const int n = size(nums1), m = size(nums2); + int i = 0, j = 0; + vector<int> res; + + sort(begin(nums1), end(nums1)); + sort(begin(nums2), end(nums2)); + while (i < n && j < m) { + if (nums1[i] == nums2[j]) { + const int crnt = nums1[i]; + res.push_back(crnt); + while (i < n && nums1[i] == crnt) + i++; + while (j < m && nums2[j] == crnt) + j++; + } else if (nums1[i] > nums2[j]) + j++; + else + i++; + } + return res; + } +}; + +class Solution { + public: + vector<int> intersection(const vector<int> &nums1, const vector<int> &nums2) const { + unordered_set<int> st(begin(nums1), end(nums1)); + vector<int> res; + for (const int n : nums2) { + if (st.erase(n)) res.push_back(n); + } + return res; + } +}; diff --git a/README.md b/README.md @@ -282,6 +282,7 @@ for solving problems. | 0344 | Easy | [Reverse String](Problems/0344.cpp) | | 0345 | Easy | [Reverse Vowels of a String](Problems/0345.cpp) | | 0347 | Medium | [Top K Frequent Elements](Problems/0347.cpp) | +| 0349 | Easy | [Intersection of Two Arrays](Problems/0349.cpp) | | 0350 | Easy | [Intersection of Two Arrays II](Problems/0350.cpp) | | 0352 | Hard | [Data Stream as Disjoint Intervals](Problems/0352.cpp) | | 0357 | Medium | [Count Numbers with Unique Digits](Problems/0357.cpp) |