<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:
    // find the maxele
    vector<int> findMax(vector<int>&v, int k) {
        // create max k digit from v
        vector<int>ans;
        if (k==0) {
            return ans;
        }
        int n = v.size();
        priority_queue<pair<int,int>>pq;
        for (int i=0; i<v.size()-k; i++) {
            pq.push({v[i], n-i});
        }
        int prevInd = -1;
        for (int i=1; i<=k; i++) {
            pq.push({v[v.size()-k+i-1], n-(v.size()-k+i-1)});
            
            while (!pq.empty() && (n-pq.top().second)<prevInd) {pq.pop();}

            pair<int,int>curr = pq.top(); pq.pop();

            ans.push_back(curr.first);
            prevInd = n-curr.second;
        }
        
        return ans;

    }

    int compareTwo(vector<int>&v1, vector<int>&v2) {
        for (int i=0; i<v1.size(); i++) {
            if (v1[i]==v2[i]) continue;
            else if (v1[i] < v2[i]) return -1;
            else return 1;
        }
        return 0;
    }

    vector<int> createMax(vector<int>&v1, vector<int>&v2) {
        int i1 = 0, i2 = 0;
        vector<int>ans;

        while (i1<v1.size() && i2<v2.size()) {
            if (v1[i1] < v2[i2]) {ans.push_back(v2[i2++]);}
            else if (v1[i1] > v2[i2]){ans.push_back(v1[i1++]);}
            else{
                int ind1 = i1, ind2 = i2;
                while (ind1<v1.size() && ind2<v2.size() && v1[ind1]==v2[ind2]) {
                    ind1++; ind2++;
                }
                cout<<ind1<<"**********"<<ind2<<endl;
                if (ind1==v1.size()){ans.push_back(v2[i2++]);}
                else if (ind2==v2.size()){ans.push_back(v1[i1++]);}
                else {
                    if (v1[ind1]<v2[ind2]) ans.push_back(v2[i2++]);
                    if (v1[ind1]>v2[ind2]) ans.push_back(v1[i1++]);
                }
            }
        }

        while (i1<v1.size()) {
            ans.push_back(v1[i1++]);
        }

        while (i2<v2.size()) {
            ans.push_back(v2[i2++]);
        }
        
        return ans;

    }

    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        vector<int>ans(k, 0);
        for (int i=0; i<=k; i++) {
            if (i<=nums1.size() && (k-i)<=nums2.size()) {
                vector<int>v1 = findMax(nums1, i);
                vector<int>v2 = findMax(nums2, k-i);
                cout<<i<<endl;
                for (auto it:v1) {cout<<it<<":";}cout<<endl;
                for (auto it:v2) {cout<<it<<":";}cout<<endl;
                vector<int>cans = createMax(v1, v2);
                int curr = compareTwo(cans, ans);
                if (curr>0) {
                    ans = cans;
                }
                for (auto it:ans){ cout<<it<<":";}cout<<endl;
            }
        }
        return ans;
    }
};
47912