<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> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {

        int n = nums1.size();
        int m = nums2.size();

        string ans = "";

        for (int i = 0; i <= k; i++) {
            if (i <= n && k - i <= m) {
                vector<int> temp1 = most(nums1, i);
                vector<int> temp2 = most(nums2, k - i);

                string s1;
                string s2;

                for (int j = 0; j < temp1.size(); j++) {
                    s1 += char(temp1[j] + '0');
                }

                for (int j = 0; j < temp2.size(); j++) {
                    s2 += char(temp2[j] + '0');
                }

                ans = max(ans, largestm(s1, s2));
            }
        }

        vector<int> sol;

        for (int i = 0; i < ans.size(); i++) {
            sol.push_back(ans[i] - '0');
        }

        return sol;
    }

    vector<int> most(vector<int>& nums, int k) {

        int n = nums.size();
        int del = n - k;

        stack<int> st;

        for (int i = 0; i < n; i++) {
            while (!st.empty() && st.top() < nums[i] && del > 0) {
                st.pop();
                del--;
            }

            st.push(nums[i]);
        }

        while (del > 0) {
            st.pop();
            del--;
        }

        vector<int> ans;

        while (!st.empty()) {
            ans.push_back(st.top());
            st.pop();
        }

        reverse(ans.begin(), ans.end());
        return ans;
    }

    string largestm(string word1, string word2) {

        string ans;

        int first = 0;
        int second = 0;

        int n = word1.size();
        int m = word2.size();

        while (first < n && second < m) {
            if (word1[first] > word2[second]) {
                ans += word1[first];
                first++;
            } else if (word1[first] < word2[second]) {
                ans += word2[second];
                second++;
            } else {
                if (word1.substr(first, n - first) >= word2.substr(second, m - second)) {
                    ans += word1[first];
                    first++;
                } else {
                    ans += word2[second];
                    second++;
                }
            }
        }

        while (first < n) {
            ans += word1[first];
            first++;
        }

        while (second < m) {
            ans += word2[second];
            second++;
        }

        return ans;
    }
};

61157