<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]:
        
        ln2=len(nums2)
        max2={}
        for i in range(1,ln2+1):
            s=[]
            ls=0
            maxpop=ln2-i
            maxi=[]
            for x in nums2:
                while maxpop>0 and s and s[-1]<x:
                    s.pop()
                    ls-=1
                    maxpop-=1
                if ls<i:
                    s.append(x)
                    ls+=1
                else:
                    maxpop-=1
                if maxi<s:
                    maxi=s.copy()
            max2[i]=maxi
        
        ln1=len(nums1)
        max1={}
        for i in range(1,ln1+1):
            s=[]
            ls=0
            maxpop=ln1-i
            maxi=[]
            for x in nums1:
                while maxpop>0 and s and s[-1]<x:
                    s.pop()
                    ls-=1
                    maxpop-=1
                if ls<i:
                    s.append(x)
                    ls+=1
                else:
                    maxpop-=1
                if maxi<s:
                    maxi=s.copy()
            max1[i]=maxi


        answer=[]
        def merge(i,j):
            nonlocal answer
            if i==0:
                if max2[j]>answer:
                    answer=max2[j].copy()
                return
            if j==0:
                if max1[i]>answer:
                    answer=max1[i].copy()
                return
            l1=max1[i]
            l2=max2[j]
            ii,jj=0,0
            l=[]
            
            while l1 or l2:
                if l1>l2:
                    l.append(l1[0])
                    l1=l1[1:]
                else:
                    l.append(l2[0])
                    l2=l2[1:]

            if l>answer:
                answer=l.copy()
        
        for i in range(ln1+1):
            if 0<=k-i<=ln2:
                merge(i,k-i)

        return answer

                
                
19200