<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> func(vector<int> &nums,int k){
    stack<int> s;
    int n1 = nums.size();
    int sz = k;
    for(int i=0;i<n1;i++){
        while(!s.empty()&&(n1-i>k)&&nums[i]>s.top()){
            s.pop();
            k++;
        }
        s.push(nums[i]);
        k--;
    }
    vector<int> v,ans;
    while(!s.empty()){
        v.push_back(s.top());
        s.pop();
    }
    reverse(v.begin(),v.end());
    for(int i=0;i<sz;i++){
        ans.push_back(v[i]);
    }
    return ans;
}

vector<int> merge(vector<int> a1,vector<int> a2){
    int i=0,j=0;
    int n = a1.size();
    int m = a2.size();
    vector<int> res;
    while(i<n&&j<m){
        if(a1[i]>a2[j]){
            res.push_back(a1[i++]);
        }
        else if(a1[i]<a2[j]){
            res.push_back(a2[j++]);
        }
        else{
            bool r = nextRight(a1,a2,i,j,n,m);
            if(r){
                res.push_back(a1[i++]);
            }
            else res.push_back(a2[j++]);
        }
    }
    while(i<n) res.push_back(a1[i++]);
    while(j<m) res.push_back(a2[j++]);

    return res;
}

bool nextRight(vector<int> a1,vector<int> a2,int i,int j,int n,int m){
    
    while(i<n&&j<m&&a1[i]==a2[j]){
        i++;
        j++;
    }
    if(i==n&&j==m) return true;
    if(i==n) return false;
    if(j==m) return true;
    return a1[i]>a2[j];
}

    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        int n = nums1.size();
        int m = nums2.size();
        vector<int> ans(k);
        for(int i=0;i<=k;i++){
            if(i>nums1.size()||(k-i)>nums2.size()) continue;

            vector<int> a1 = func(nums1,i);
            vector<int> a2 = func(nums2,k-i);

            vector<int> a = merge(a1,a2);
            // for(auto it:a) cout<<it<<" ";
            // cout<<"--*\n";
            ans = max(ans,a);
        }
        return ans;
    }
};
149457