<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 {
public:
    vector<int> maxArray(vector<int> arr,int len){
        stack<int> st;
        int i=0,n=arr.size();
        while(i<n){
            while(!st.empty() && st.size()+n-i>len && arr[i]>st.top()){
                 st.pop();
            }
            if(st.size()<len)st.push(arr[i]);
            i++;
        }
        vector<int> result(len);
        int j=len-1;
        while(!st.empty()){
           result[j]=st.top();
           st.pop();
           j--;
        }
        return result;

    }
    bool greater(vector<int> ans,int i,vector<int> finalans,int j){
       
        int i1=i,i2=j;
        int n1=ans.size();
        int n2=finalans.size();
        while(i1<n1 && i2<n2){
            if(ans[i1]==finalans[i2]){
                 i1++;i2++;
            }
            else if(ans[i1]>finalans[i2]) return true;
            else return false;
        }
        return i1<n1;
    }

    vector<int> merge(vector<int> arr1, vector<int> arr2){
        int i1=0,i2=0;
        int n1=arr1.size();
        int n2=arr2.size();
        vector<int> result(n1+n2);
        int j=0;
        while(i1<n1 && i2<n2){
            if(arr1[i1]>arr2[i2]){
                result[j]=arr1[i1];
                i1++;
            }else if(arr1[i1]<arr2[i2]){
                result[j]=arr2[i2];
                i2++;
            }
            else{
                if(greater(arr1,i1,arr2,i2)){
                   result[j]=arr1[i1];
                   i1++;
                }else{
                   result[j]=arr2[i2];
                   i2++;
                }
            }
            j++;
        }
        while(i1<n1){
            result[j]=arr1[i1];
            i1++;
            j++;
        }
        while(i2<n2){
            result[j]=arr2[i2];
            i2++;
            j++;
        }
        return result;

    }
    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        vector<int> finalans(k,0);
        for(int i=0;i<=k;i++){
            int s1=i;
            int s2=k-i;
            if(s1>nums1.size() || s2>nums2.size())continue;
            vector<int> ans=merge(maxArray(nums1,s1),maxArray(nums2,s2));
            if(greater(ans,0,finalans,0))finalans=ans;         
        }
        return finalans;
    }
};
140627