4. Median of Two Sorted Arrays | Hard
# there must be a better way, below is O(n)
# also for it to work, I would need to check bounds
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
l1, r1 = 0, len(nums1) - 1
l2, r2 = 0, len(nums2) - 1
odd = (len(nums1) + len(nums2)) % 2
cur = 0
medIndex1 = ((len(nums1) + len(nums2))/2 + 0.5)//1 - 1
medIndex2 = ((len(nums1) + len(nums2))/2 + 0.5)//1
median = 0
# for loop may not work if either list is empty
# assumed this is not the case via problem statement
for i in range(len(nums1)+len(nums2)):
if i == medIndex1:
median += nums2[l2] if cur else nums1[l1]
elif not odd and i == mexIndex2:
median += nums2[l2] if cur else nums1[l1]
if nums1[l1] < nums2[l2]:
l1 += 1
cur = 0
elif nums1[l1] >= nums2[l2]:
l2 += 1
cur = 1
return median if odd else median / 2