Since the input size and range are small, this is easier and more efficient: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: arr = [0] * 1001 res = [] for n in nums1: arr[n] = 1 for n in nums2: if arr[n] == 1: res.append(n) arr[n] = 0 return res
@@coffeebytes3257personally I think either way works, the most important part is getting a constant lookup while checking if the number is in a set lol
I wonder if my solution in Swift would be accepted by an interviewer let set1 = Set(nums1) let set2 = Set(nums2) return Array(set1.intersection(set2)) it is still O(n)
you have the best solution, the problem is: that the interviewer always asks you for a classic Big O(Nˆ2) solution at the beginning and then, once you solved, will ask you about an optimization, here is when you can show your optimal solution using set theory.
You're like the last few pages of the book that gives all the answers
When I solved this issue on my own I didn't come to the idea of delete item from set. It's very graceful move. Thank you!
Hello!!!
Could you please make a video series on CSES Problem Set?
Sir you didn't upload today's streak question solution😢
Neethcode bro where are from last 2 days. I need some videos for last 2 problems pls .....
Thnx in advance
Since the input size and range are small, this is easier and more efficient:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
arr = [0] * 1001
res = []
for n in nums1:
arr[n] = 1
for n in nums2:
if arr[n] == 1:
res.append(n)
arr[n] = 0
return res
Thank you NeetCode
Is it possible to buy premium from Europe ? Will entering US address and buying via Cash App work ?
Can't we just convert them to 2 sets and perform & over them
like
return set(nums1) & set(nums2)
too inefficient. Takes O(n) to build each of the sets and takes up too much space.
@@emenikeanigbogu9368 still O(n) time & space lol my solution was a second slower.
had the same question, I had two sets and iterated over the first set and checked if the num was also in set 2 then appended to the array
@@coffeebytes3257personally I think either way works, the most important part is getting a constant lookup while checking if the number is in a set lol
return set(nums1) & set(nums2)
bruh
I wonder if my solution in Swift would be accepted by an interviewer
let set1 = Set(nums1)
let set2 = Set(nums2)
return Array(set1.intersection(set2))
it is still O(n)
you have the best solution, the problem is: that the interviewer always asks you for a classic Big O(Nˆ2) solution at the beginning and then, once you solved, will ask you about an optimization, here is when you can show your optimal solution using set theory.
1 - row solution : ) Like is guaranted
I got a faster time by breaking out early when the set is empty.
True, but code complexity remains same. Cos you can add the same logic to inputs
if (!num11.length && !num2 length) return []
How come he sees the cnt of dislikes, if that premium only is yes then that's absurd lmfaooo 1:08
Easy
Most optimal solutionfor Rust was just take two sets of those two input vectors and just call intersect
Bro, what happened to you? You literally died for 4 days
All questions had solutions on this channel already, so making another video on them would be redundant
return list(set(nums1).intersection(set(nums2)))
O(max{O(n log(n) + m log(m)), min{n, m})/O(len(ans)) soluton;
class Solution {
public:
vector intersection(vector& nums1, vector& nums2) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::sort(nums1.begin(), nums1.end());
std::sort(nums2.begin(), nums2.end());
int l = 0, r = 0;
std::vector ans;
while(l < nums1.size() and r < nums2.size()) {
if(nums1[l] == nums2[r]) {
if(ans.empty()) {
ans.push_back(nums1[l]);
} else {
if(nums1[l] != ans.back()) {
ans.push_back(nums1[l]);
}
}
++l; ++r;
} else if(nums1[l] < nums2[r]) {
++l;
} else {
++r;
}
// if(l >= nums1.size() or r >= nums2.size()) { break; } // mostly ineffective
}
return ans;
}
};
Brother, teach design patterns for free
Sanjay