Binary Search : Median of two sorted arrays of different sizes.

preview_player
Показать описание
In example 1 end should be 5(not 4) since there are total 5 elements.
Find median of two sorted arrays of different sizes. Algorithm is based on binary search.
Рекомендации по теме
Комментарии
Автор

I love when he says "once it hits you" like some kind of drug or something

shrimatkapoor
Автор

Man the way you explained it in the first 5 minutes just clarified everything. Thanks. Great video!

nik
Автор

Clear explanation. Thanks.  
A small error, 8:33 value of "end" should be 5 (size of small array). I followed just your example and coded, everything was right but failing always, until I watched your code & realized that end should have initial value of smallest array's length.

balajim
Автор

I paused the video at 20:12 and now going to solve the problem at leetcode myself, thank you so much Tushar, your help for the rest of us who have never graduated from the CS but still want to become a sane programmer is invaluable

barmalini
Автор

Damn !!! How did anyone come up with such an Algorithm

dakshays
Автор

I have seen many other videos for this problem, but yours is the one that made me follow every step clearly. Thank you!

rushivt
Автор

Brilliant video and explanation!
One minor change to the 1st example:initialization:
1) pick smaller len
2) start = 0, end = len, binary search the len of left size of X
e.g with the first example:
1st round lo = 0, hi = 5, mid = lo + (hi - lo) / 2 = 2
2nd round lo = mid + 1 = 3, hi = 5

meganlee
Автор

This is amazing. I was so confused by this problem, you explained it so succinctly. Thanks

beer_bandhu
Автор

Even after 5 years your video is best for this problem.Thank you so much for wonderful Explaination

shaziasamreen
Автор

I was plagued by this problem for a very long time, you made it crystal clear, sir. Thank you!

程龙-bw
Автор

came to "like" this video only found out that I have "liked" this video 4 years ago when I was looking for my last job. Thank you Tushar.

jessica_
Автор

Perfect explanation, initially I though it would be difficult for me. But gradually you made me understand in only one go.

Thanks 😀

ADITYAKUMAR-ydow
Автор

7:18 end should be 5 not 4 because we are searching which position we want to cut.

kimmeng
Автор

The best explaination with perfect testcases that I found for this question on Internet.
Thank you!

somiljain
Автор

Tushar, thank you so much! You explained it perfectly!
If someone need solution in Python, here it is:
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
if len(nums1) > len(nums2):
return self.findMedianSortedArrays(nums2, nums1);

x = len(nums1)
y = len(nums2)

low = 0
high = x
while low <= high:
partitionX = (low + high)//2
partitionY = (x + y + 1)//2 - partitionX

#if partitionX is 0 it means nothing is there on left side. Use -INF for maxLeftX
#if partitionX is length of input then there is nothing on right side. Use +INF for minRightX
maxLeftX = -(2 ** 31) if partitionX == 0 else nums1[partitionX - 1]
minRightX = (2 ** 31 - 1) if partitionX == x else nums1[partitionX]

maxLeftY = -(2 ** 31) if partitionY == 0 else nums2[partitionY - 1]
minRightY = (2 ** 31 - 1) if partitionY == y else nums2[partitionY]

if maxLeftX <= minRightY and maxLeftY <= minRightX:
#We have partitioned array at correct place
#Now get max of left elements and min of right elements to get the median in case of even length combined array size
#or get max of left for odd length combined array size.
if (x + y) % 2 == 0:
return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY))/2

else:
return max(maxLeftX, maxLeftY)

elif maxLeftX > minRightY:
#we are too far on right side for partitionX. Go on left side.
high = partitionX - 1
else:
#we are too far on left side for partitionX. Go on right side.
low = partitionX + 1

DonchenkoNadia
Автор

People like me wouldnt understand this video in one time, you gotta watch it multiple times, read about the problem solving strategy then you will get the problem.
He has explained in a very good manner

umairalvi
Автор

00:00 Introduction
01:39 Solution
06:19 Example 1
14:35 Example 2
20:11 Code

uzdik.student
Автор

this is the best video of all time, it brings happiness. thank you!

marshallxu
Автор

I went through 5 videos before landing here and finally understanding the intuition behind the solution. Thank you!

sanjitselvan
Автор

This video is definitely the most effective one in providing a clear explanation of the algorithm

wengao
welcome to shbcf.ru