###########################################################################
#                                                                         #
# Author: Daniel Mock                                                     #
#                                                                         #
# Purpose: To document progress in learning algorithms & data structures  #
#                                                                         #
# References: This question was generated by leetcode.com. The solution   #
# to the question was generated by Daniel Mock.                           #
#                                                                         #
###########################################################################

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        len_1 = len(nums1)
        len_2 = len(nums2)

        if not nums1:
            if len_2 == 1: return nums2[0]
            target_1 = (len_2 // 2) - 1
            target_2 = target_1 + 1
            if len_2 % 2 == 0:
                return (nums2[target_1] + nums2[target_2]) / 2
            else:
                return nums2[target_2]

        if not nums2:
            if len_1 == 1: return nums1[0]
            target_1 = (len_1 // 2) - 1
            target_2 = target_1 + 1
            if len_1 % 2 == 0:
                return (nums1[target_1] + nums1[target_2]) / 2
            else:
                return nums1[target_2]

        mid = len_1 + len_2

        target_1 = mid//2
        target_2 = target_1 + 1
        new_arr = []

        for _ in range(target_2):
            if not nums1:
                new_arr.append(nums2.pop(0))
            elif not nums2:
                new_arr.append(nums1.pop(0))
            elif nums1[0] <= nums2[0]:
                new_arr.append(nums1.pop(0))
            else:
                new_arr.append(nums2.pop(0))
        num1, num2 = new_arr[-2], new_arr[-1]
        if mid % 2 == 0:
            #even, get average of mid//2 & mid//2+1
            val = (num1 + num2) / 2
            return val

        else:
            #odd, get mid//2+1
            return num2
