2
def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:7
# get the max number string with "length" from index "i" in nums1 and index "j" in nums28
# using number string to easy to compare9
def getMaxNumberString(i, j, length):13
# using memoization to optimize for the overlapping subproblems18
# greedy to find the possible max digit from nums1 and nums219
# 1) bigger digit in the higher position of the number will get bigger number20
# 2) at the same time, we need to ensure that we still have enough digits to form a number with "length" digits22
# try to find the possible max digit from index i in nums124
for ii in range(i, m):25
if (m - ii + n - j) < length:27
if index1 is None or nums1[index1] < nums1[ii]:30
# try to find the possible max digit from index j in nums232
for jj in range(j, n):33
if (m - i + n - jj) < length:35
if index2 is None or nums2[index2] < nums2[jj]:39
if index1 is not None and index2 is not None:40
if nums1[index1] > nums2[index2]:41
maxNumberStr = str(nums1[index1]) + getMaxNumberString(42
index1 + 1, j, length - 144
elif nums1[index1] < nums2[index2]:45
maxNumberStr = str(nums2[index2]) + getMaxNumberString(46
i, index2 + 1, length - 149
# get the same digit from nums1 and nums2, so need to try two cases and get the max one52
+ getMaxNumberString(index1 + 1, j, length - 1),54
+ getMaxNumberString(i, index2 + 1, length - 1),56
elif index1 is not None:57
maxNumberStr = str(nums1[index1]) + getMaxNumberString(58
index1 + 1, j, length - 160
elif index2 is not None:61
maxNumberStr = str(nums2[index2]) + getMaxNumberString(62
i, index2 + 1, length - 165
dp[key] = maxNumberStr68
result_str = getMaxNumberString(0, 0, k)70
# number string to digit array