LeetCode 350 | Intersection of Two Arrays II | Solution Explained (Java + Whiteboard)

preview_player
Показать описание
Today we go over the sort method to solving this problem!

Running Time: O(mlogm + nlogn)
Space Complexity: O(logm + logn)

Always be pluggin:
Рекомендации по теме
Комментарии
Автор

Thank you!!! Finally, this was the first explanation I was actually able to understand.

fsxaviator
Автор

Hey. The solution doesn't work for : int[] arr1 = {4, 9, 5};
int[] arr2 = {9, 4, 9, 8, 4}; please check.

PriyamF
Автор

Thank you. I have always wondered why there was a pre-increment and a post increment, and this is a perfect use case for post-increment. It's the small things sometimes. I've never seen that before.

txjzidn
Автор

Finally an explanation using constant space!

sayantaniguha
Автор

Keep up the good work! You're doing great.

HenggaoCai
Автор

I had it so close.... I was increasing the indexes depending on nums1 and nums2 sizes... that approach worked for a few testcases but I couldn't figure out until saw this video, thanks! For sure I was not able to solve it by myself but still learned a lot :)

ritchievales
Автор

At max, minimum of size of both arrays can be duplicates, so instead of doing length1 * length2, you can do min(length1, length2)

athangkulkarni
Автор

Great video, you definitely deserve more subs, one note: that music started waaaayyy too early and was kinda unexpected covering your voice over.

sasgaro
Автор

why did we use nums1 and not nums2 to store the common elements

bina.soche.samjhe
Автор

Great explanation. Just wasn't clear to me why SC is O(logm + long). Is it due to the sort of arrays generates overrides the initial value of nums array or why? Thanks in advance.

pubamx
Автор

how to implement the same in c++ with same space com.
i tried but more elements are getting copied can you give code for same. thanks for video

AbhishekKumar-ffvg
Автор

What about the values which are already present in nums1 how are they getting deleted

adithyang
Автор

Solution without using extra space is amazing thanks! Bit time complexity basically is O(N), isn't it?

lighto
Автор

I am little confused about the third array(ans) size as length1 * length2. Can we take the size as the smallest of length1 and length2?

monikajha
Автор

How is the space *O(log m + log n)* ?

sayantaniguha
Автор

where are you getting the nums in return Arrays.copyOfRange(nums, 0, k);

odongoemmanuel
Автор

Am I missing anything, I feel this algorithm is not working.
A=[1, 2, 2, 1, 3, 2]
B=[1, 2, 3]
A_sorted=[1, 1, 2, 2, 2, 3]
expected output: [1, 2]
The Algorithm will produce[1, 2, 3]

qianjun
Автор

Which software do you use as a whiteboard??
thanks

pqr.priyanshu
Автор

So smart to add background music while you're unable to articulate well the n/m complexity. I've broken my ears

artemvolkov