<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> findMaxNum(vector<int>& nums, int k) {
       stack<int> maxSequence;
       int n = nums.size();
       for(int i = 0; i < nums.size(); i++){
           //Pop out so that we can replace the current number
           int numLeft = n - i;
           while(!maxSequence.empty() && nums[i] > maxSequence.top() && numLeft + maxSequence.size() > k){
               maxSequence.pop();
           }
           if (maxSequence.size() < k){
               maxSequence.push(nums[i]);
           }
       }
       vector<int> ans(min(k, n));
       int insertIndex = ans.size() - 1;
       while(!maxSequence.empty()){
           ans[insertIndex--] = maxSequence.top();
           maxSequence.pop();
       }
       return ans;
   }
   string mergeMaxNums(vector<int> &maxNum1, vector<int> &maxNum2, int k){
        int n = maxNum1.size(), m = maxNum2.size();
        int p1 = 0, p2 = 0;
        string ans = "";
        while (p1 < n && p2 < m && ans.size() < k){
            int num1 = maxNum1[p1];
            int num2 = maxNum2[p2];
            if (num1 > num2){
                ans += to_string(num1);
                p1++;
            } else if (num1 == num2){
                //choose whichever pointer has the next greatest number
                int p1Ptr = p1, p2Ptr = p2;
                while(p1Ptr < n && p2Ptr < m && maxNum1[p1Ptr] == maxNum2[p2Ptr]){
                    p1Ptr++;
                    p2Ptr++;
                }
                if (p1Ptr == n){
                    ans += to_string(num2);
                    p2++;
                } else if (p2Ptr == m || maxNum1[p1Ptr] > maxNum2[p2Ptr]){
                    ans += to_string(num1);
                    p1++;
                } else {
                    ans += to_string(num2);
                    p2++;
                }
            }
            else {
                ans += to_string(num2);
                p2++;
            }
        }
        while (p1 < n && ans.size() < k){
            ans += to_string(maxNum1[p1++]);
        }
        while (p2 < m && ans.size() < k){
            ans += to_string(maxNum2[p2++]);
        }
        return ans;
   }

    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        string maxNum = "";
        int n = nums1.size(), m = nums2.size();
        for(int sz = 0; sz <= min(k, n); sz++){
            vector<int> maxNum1 = findMaxNum(nums1, sz);
            vector<int> maxNum2 = findMaxNum(nums2, k-sz);
            string res = mergeMaxNums(maxNum1, maxNum2, k);
            if (res.size() > maxNum.size()){
                maxNum = res;
                continue;
            }
            maxNum = max(maxNum, res);
        }
        vector<int> ans;
        for(const auto& c : maxNum){
            ans.push_back((int)(c-'0'));
        }
        return ans;
    }
};
56742