➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Intersection of Two Arrays II

image

This is part two of the Intersection of Two Arrays

The difference here is that each element in the result must appear as many times as it shows in both arrays

So let us consider this two arrays:

num1 = [1, 3, 5 3]
num2 = [3, 3]

The result we should be expecting is [3, 3] and why?

Well I solved mine based on how many times can each element in num2 appear in num1

It can be vice versa but more ideal to use the array of lower size

I saw that my previous Overkill script takes too much in terms of speed

So I decided to optimize using Hash table

The idea is this:

With that this program is way more optimized than before :P image

But in terms of Space Complexity the Big O is O(min(N, M)), where N is the length of nums1 and M is the length of nums2.

Solve Script: link