当前位置:网站首页>LeetCode 1673. Find the most competitive subsequence**

LeetCode 1673. Find the most competitive subsequence**

2022-06-11 00:14:00 Evening rain forest bell

Specific ideas :

and 321 The problem is the same as the solution ;

Is the precursor scheme ;

It is equivalent to finding a minimum non strict ascending sequence , Keep front-end elements as small as possible ;

index Represents the number of bits saved ;

remain Represents the number to be deleted ;

So pass remain To make sure , If you want to keep the front-end elements as small as possible , How many can be deleted at most ;

Specific code :

class Solution {
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        int n=nums.size();
        vector<int>st;
        int remain=n-k;
        int index=0;
        for(int i=0;i<nums.size();i++){
            while(!st.empty()&&nums[i]<*st.rbegin()&&remain>0){
                index--;
                remain--;
                st.pop_back();
            }
            if(index<k){
                st.push_back(nums[i]);
                index++;
            }else{
                remain--;
            }
        }
        return st;
    }
};

//2,3,3,4,6
原网站

版权声明
本文为[Evening rain forest bell]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206102257282918.html