LeetCode 350: Intersection of Two Arrays II - Interview Prep Ep 50

preview_player
Показать описание


⭐ Support my channel and connect with me:

Solutions explained:

Solution 1. We could use one hash map to store all the counts of each element in the shorter array, then loop through the second array to find all possible intersection and return;
Quick tip: you want to always build your hash map on top of the shorter array, so that the space complexity could be minimized.

Solution 2. We can sort both arrays first, then use two pointers to loop through both sorted arrays, whenever we encounter two elements from each array that equal, we'll add it to the final result.

// TOOLS THAT I USE:

// MY FAVORITE BOOKS:

My ENTIRE Programming Equipment and Computer Science Bookshelf:

And make sure you subscribe to my channel!

Your comments/thoughts/questions/advice will be greatly appreciated!

#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Рекомендации по теме
Комментарии
Автор

I'm a student at uni. I have received a lot of valuable tech from your code, which has not ever existed in my mind. Thank you very much!

My.Daisy.
Автор

Thank you very much! I like your explanation a lot! One of the go-to Youtuber for LeetCode problems!

honglingliu
Автор

Thanks for this. While I couldn't really understand the Java, I was able to follow along and solve it with Python just based on you explaining the reasoning of each step, what we needed to do. Very very helpful.

minutesdead
Автор

Misunderstood problem as LCS and wasted a lot of time. Turns out its more simpler than I thought. Great video btw. Thanks!

nullentrophy
Автор

You are insanely good at explaining this! Thank you!

wishimaunicorn
Автор

I fell off of my chair when the submission failed. Nice sound effect 😁

anikkhan
Автор

This is super helpful and clear! Thanks for clearly explaining all the follow up questions as well

Josh-ejzm
Автор

that was hilarious when the code didn't pass first time, thank you so much, your videos are super helpful.

MiddleEasternInAmerica
Автор

Thanks for doing the follow ups, you're the MVP

jacobr
Автор

Hi, just a quick question.
Why is the second solution's Space Complexity O(1)?
How should the ArrayList be considered?
Thank you. Your videos have been so helpful by the way.

jingyulee
Автор

Thanks for the video. I loved the explanation and complexity analysis with the approaches

AnkitPawar
Автор

Hello! Can you please explain why we used List and not a map for the nums2, we are supposed to map nums2 as well right and if we are using list for nums2 then why not for nums1 ? You did in the video but I did not understand. Thank you.

xxMeghaxx
Автор

line number 8 should be map.getOrDefault(num, 1)+1 right instead of map.getOrDefault(0, 1)+1? I dont understand why 0 is used as an argument.

sysybaba
Автор

Which is the best Algorithm here? in terms of time the first one and in terms of space the second.. but how to decide?

janeshwarsritharan
Автор

Correct me if I'm wrong but it seems like in the second solution while loop is O(N) complexity which is greater than O(logN)

lighto
Автор

Hi, how could we solve it by Binary Search approach?

rishabh
Автор

Great explanation 👍🏻
Btw what distro are you using ?

woodsjr
Автор

can you further explain the limited memory question? - breaking nums 2 into chunks

malcolmparrish
Автор

Real good explanation. But you should work on quality of your videos: video in 720P is not enough for comfortable watching on big screen and sound effects are not needed, they are too loud.

ImranAliyev
Автор

class Solution {
public int[] intersect(int[] nums1, int[] nums2) {

return intersect(nums2, nums1);

Map<Integer, Integer> map = new HashMap();

for(int i : nums1){
map.put(i, map.getOrDefault(i, 0)+1);
}

List<Integer> list = new ArrayList();

for(int i : nums2){
int count = map.getOrDefault(i, 0);
if(count>0){
list.add(i);
map.put(i, count-1);
}
}

int[] ans = new int[list.size()];

int j = 0;
for(int i : list)
ans[j++] = i;
return ans;
}
}

prakashkrishna
join shbcf.ru