<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> get(vector<int> a, int len) {
        int needlen = len;
        int n = a.size();
        vector<vector<int> > ne(n + 1, vector<int> (10, -1));
        for (int i = n - 1; i >= 0; --i) {
            for (int j = 0; j <= 9; ++j) ne[i][j] = ne[i + 1][j];
            ne[i][a[i]] = i;
        }
        vector<int> ans;
        int cur = 0;
        while (len > 0) {
            bool ok = false;
            for (int dig = 9; dig >= 0; --dig) {
                int id = ne[cur][dig];
                if (id == -1) continue;
                int mx = (int)ans.size() + (int)a.size() - id;
                if (mx < needlen) continue;
                cur = id + 1;
                ans.push_back(dig);
                ok = true;
                break;
            }
            --len;
        }
        return ans;
    }

    vector<int> merge(vector<int> a, vector<int> b) {
        vector<int> ans;
        a.push_back(-1);
        b.push_back(-1);
        int i = 0, j = 0;
        while (i + 1 < a.size() && j + 1 < b.size()) {
            if (a[i] > b[j]) {
                ans.push_back(a[i]);
                ++i;
            } else if (a[i] < b[j]) {
                ans.push_back(b[j]);
                ++j;
            } else {
                bool ok = false;
                for (int k = 1; (i + k < a.size()) && (j + k < b.size()); ++k) {
                    if (a[i + k] != b[j + k]) {
                        ok = a[i + k] > b[j + k];
                        break;
                    }
                }
                if (ok) {
                    ans.push_back(a[i]);
                    ++i;
                } else {
                    ans.push_back(b[j]);
                    ++j;
                }
            }
        }
        while (i + 1 < a.size()) ans.push_back(a[i]), ++i;
        while (j + 1 < b.size()) ans.push_back(b[j]), ++j;
        return ans;
    }

    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        vector<int> ans;
        for (int len1 = 0; len1 <= k; ++len1) {
            int len2 = k - len1;
            if (len1 > nums1.size() || len2 > nums2.size()) continue;
            vector<int> best1 = get(nums1, len1);
            vector<int> best2 = get(nums2, len2);
            vector<int> cur = merge(best1, best2);
            if (ans.empty() || cur > ans) ans = cur;
        }    
        return ans;
    }
};
136212