<p>Given an integer array <code>nums</code> and a positive integer <code>k</code>, return <em>the most<strong> competitive</strong> subsequence of </em><code>nums</code> <em>of size </em><code>k</code>.</p>

<p>An array&#39;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.</p>

<p>We define that a subsequence <code>a</code> is more <strong>competitive</strong> than a subsequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, subsequence <code>a</code> has a number <strong>less</strong> than the corresponding number in <code>b</code>. For example, <code>[1,3,4]</code> is more competitive than <code>[1,3,5]</code> because the first position they differ is at the final number, and <code>4</code> is less than <code>5</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,5,2,6], k = 2
<strong>Output:</strong> [2,6]
<strong>Explanation:</strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,3,3,5,4,9,6], k = 4
<strong>Output:</strong> [2,3,3,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution {
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        // cout<<"----"<<endl;
        stack<pair<int, int>>stk;
        int n = nums.size();
        for(int i=0; i<n; i++) {
            if (stk.empty()) {
                stk.push({nums[i], i});
            } else {
                while(!stk.empty() && stk.top().first > nums[i]) {
                    stk.pop();
                }
                stk.push({nums[i], i});
            }
        }
        vector<int> ans(k);
        if (stk.size() >= k) {
            while(stk.size() > k) stk.pop();
            int i = k-1;
            while(!stk.empty()) {
                ans[i--] = stk.top().first;
                stk.pop();
            } 
        } else {
            int r = n-1, index = k-1;
            int cnt = k - stk.size();
            while(cnt > 0 && !stk.empty()) {
                auto node = stk.top();
                // cout<<"node: "<<node.first<<" "<<node.second<<" r: "<<r<<" cnt: "<<cnt<<endl;
                if (r - node.second <= cnt) {
                    cnt -= (r-node.second);
                    while(r >= node.second) {
                        ans[index--] = nums[r--];
                    }
                    // cout<<"new cnt: "<<cnt<<endl;
                    stk.pop();
                } else {
                    vector<int>temp = vector<int>(nums.begin()+node.second+1, nums.begin()+r+1);
                    vector<int> res = mostCompetitive(temp, cnt);
                    r = res.size()-1;
                    while(r >= 0) {
                        ans[index--] = res[r--];
                    }
                    cnt = 0;
                }
            }
            if (stk.empty() && cnt > 0) {
                vector<int>temp = vector<int>(nums.begin(), nums.begin()+r+1);
                vector<int> res = mostCompetitive(temp, cnt);
                r = res.size()-1;
                while(r >= 0) {
                    ans[index--] = res[r--];
                }
            } else {
                while(!stk.empty()) {
                    ans[index--] = stk.top().first;
                    stk.pop();
                }
            }
        }
        return ans;
    }
};
121200