<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of lengths <code>m</code> and <code>n</code> respectively. <code>nums1</code> and <code>nums2</code> represent the digits of two numbers. You are also given an integer <code>k</code>.</p>

<p>Create the maximum number of length <code>k &lt;= m + n</code> from digits of the two numbers. The relative order of the digits from the same array must be preserved.</p>

<p>Return an array of the <code>k</code> digits representing the answer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
<strong>Output:</strong> [9,8,6,5,3]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [6,7], nums2 = [6,0,4], k = 5
<strong>Output:</strong> [6,7,6,0,4]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [3,9], nums2 = [8,9], k = 3
<strong>Output:</strong> [9,8,9]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == nums1.length</code></li>
	<li><code>n == nums2.length</code></li>
	<li><code>1 &lt;= m, n &lt;= 500</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
	<li><code>1 &lt;= k &lt;= m + n</code></li>
</ul>

class Solution:
    def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:
        nums1_max = self.singleArraryMaxIndex(nums1)
        nums2_max = self.singleArraryMaxIndex(nums2)
        res = []
        for i in range(min(k + 1, len(nums1) + 1)):
            res1 = self.singleMaxK(nums1, nums1_max, i)
            res2 = self.singleMaxK(nums2, nums2_max, k - i)
            temp = self.constructArray(res1, res2)
            if len(res) == 0:
                res = temp[:]
            else:
                temp_n = self.arrayToInt(temp)
                res_n = self.arrayToInt(res)
                if temp_n > res_n:
                    res = temp[:]
        return res

    def arrayToInt(self, nums: List[int]) -> int:
        res = 0
        for i in nums:
            res = res * 10 + i
        return res

    def constructArray(self, nums1: List[int], nums2: List[int]) -> List[int]:
        i1, i2 = 0, 0
        n1, n2 = len(nums1), len(nums2)
        res = []
        while i1 < n1 or i2 < n2:
            if i1 < n1 and i2 < n2:
                if nums1[i1] > nums2[i2]:
                    res.append(nums1[i1])
                    i1 += 1
                elif nums1[i1] < nums2[i2]:
                    res.append(nums2[i2])
                    i2 += 1
                else:
                    temp_i1, temp_i2 = i1+1, i2+1
                    while temp_i1 < n1 and temp_i2 < n2 and nums1[temp_i1] == nums2[temp_i2]:
                        temp_i1 += 1
                        temp_i2 += 1
                    if temp_i1 < n1 and temp_i2 < n2:
                        if nums1[temp_i1] > nums2[temp_i2]:
                            res.append(nums1[i1])
                            i1 += 1
                        else:
                            res.append(nums2[i2])
                            i2 += 1
                    elif temp_i1 < n1:
                        res.append(nums1[i1])
                        i1 += 1
                    else:
                        res.append(nums2[i2])
                        i2 += 1
            elif i1 < n1:
                res.append(nums1[i1])
                i1 += 1
            else:
                res.append(nums2[i2])
                i2 += 1
        return res

    def singleMaxK(self, nums: List[int], max_index: List[List[int]], k: int) -> List[int]:
        n = len(nums)
        res = []
        start, end = 0, n - k
        while len(res) < k and start <= end:
            res.append(nums[max_index[start][end]])
            start = max_index[start][end] + 1
            end += 1
        return res

    def singleArraryMaxIndex(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        res = [[0] * n for i in range(n)]
        for i in range(n):
            for j in range(i, n):
                if j == i:
                    res[i][j] = i
                else:
                    res[i][j] = j if nums[j] > nums[res[i][j-1]] else res[i][j-1]
        return res
20600