Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Logic:
- We know the size of both the arrays so if we merge both the arrays and break the combined arrays in two half,then there will be some elements from array1 and some elements from array2.we can take zero elements from an array also,But one thing for sure that each half will be (n1+n2)/2 lets think about even count now.
- for eg. arr1: [1,3,4,7,10,12] ->size=6
- arr2: [2,3,6,15] ->size=4
- so total size of combined array=10,size of each half=10/2=5
- so let's assume 3 element from first array in first half,then only 2 elements will be from
- second array in first half and if we have first half,then we can find maximum element from
- first half and minimum from second half for median.
- So,now basically the problem is divide the first array such that left side elements of the
- array must belong to the actual combined array first half.
- suppose if be take 2 elements from arr1 then first half of combined array will be 1,3 and
- 2,3,6
- eg. 1 3 | 4 6 10 12
- 2 3 6 | 15
- so last element of first partition of first array will always be smaller than the
- the first element of second half because they belong to same array.same for second array.
- but if 1 3 and 2 3 6 is actually the first half of combined array then 3 should also be smaller
- than 15 and 6 also should smaller than 4 becuase whatever in first array should be smaller than
- all elements of second array or can be equal to the right array elements in case of duplication.If it happend we got the actual partition.
- We apply binary search for finding the actual cut1.
- so final question is - Where to place the cut1?
- Trick:
- 1)Alway apply the cut1 on the shorter size array?
- 2)Adjust the cut1 range, not the cut1 directly
- 3)total number of elmenets in the left side would (n1+n2)//2 or just one more in case of odd so use (n1+n2+1)//2 to find the elements needed to put in the left array of the sorted combined array and subtract cut1 from this to find the elements needed in second left array.
- 4)cut1 denotes the how many elements we are choosing from the first array so it can be 0 to all so starting space of binary search will be l=0 to r=len(arr1), no need to make it 0 based indexing.
- Why we are applyting cut1 on the smaller size list?
- -suppose first array size is 100 and second is only 2 and why applying the binary search, you somehow make cut1 as 10 then ideally left side have (100+2)//2 ie. 51 but second array have only 2 elements so it wll give index out of range so apply on the smaller list, even you choose cut1=0, second array will compensate the elemnts or even you chose cut1=len(arr1) still cut2 will compensate.
- '''
- class Solution:
- def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
- # Always binary search on the smaller array
- if len(nums1) > len(nums2):
- nums1,nums2=nums2,nums1
- n1 = len(nums1)
- n2 = len(nums2)
- low, high = 0, n1
- while low <= high:
- cut1 = (low + high) // 2
- cut2 = (n1 + n2 + 1) // 2 - cut1
- left1 = -inf if cut1 == 0 else nums1[cut1 - 1]
- left2 = -inf if cut2 == 0 else nums2[cut2 - 1]
- right1 = inf if cut1 == n1 else nums1[cut1]
- right2 = inf if cut2 == n2 else nums2[cut2]
- if left1 <= right2 and left2 <= right1:
- if (n1 + n2) % 2 == 1:
- return max(left1, left2)
- else:
- return (max(left1, left2) + min(right1, right2)) / 2.0
- elif left1 > right2:
- high = cut1 - 1
- else:
- low = cut1 + 1
- return 0.0
Advertisement
Add Comment
Please, Sign In to add comment