<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]:

        g  =[]
        n1,n2=len(nums1),len(nums2)
        nums1.append(inf)
        nums2.append(inf)
        for w in range(k+1):
            u1,u2 = w,k-w
            cs = []
            if u1<=n1 and u2<=n2:
                st1 = deque([])
                st2 = deque([])
                d1,d2 = n1-u1,n2-u2
                for num in nums1:
                    while st1 and d1>0 and num>st1[-1]:
                        st1.pop()
                        d1-=1
                    st1.append(num)
                st1.pop()
                for num in nums2:
                    while st2 and d2>0 and num>st2[-1]:
                        st2.pop()
                        d2-=1
                    st2.append(num)
                st2.pop()
                while st1 or st2:
                    A = st1 if st1>st2 else st2
                    cs.append(A.popleft())
                # i1,i2=0,0
                # while i1<len(st1) or i2<len(st2):
                #     if i2==len(st2) or i1<len(st1) and st1[i1]>st2[i2]:
                #         cs.append(st1[i1])
                #         i1+=1
                #     else:
                #         cs.append(st2[i2])
                #         i2+=1
                g.append(cs)
        maxi = [0]*k
        for cur in g:
            flag = False
            for u,v in zip(cur,maxi):
                if u>v:
                    flag=True
                    break
                if u<v: break
            if flag:
                maxi=cur
        return maxi



        
18400