1673.cpp (405B)
1 class Solution { 2 public: 3 vector<int> mostCompetitive(const vector<int> &nums, int k) const { 4 vector<int> st; 5 st.reserve(k); 6 7 for (int i = 0; i < size(nums); i++) { 8 while (!st.empty() && k - size(st) < size(nums) - i && st.back() > nums[i]) 9 st.pop_back(); 10 if (size(st) < k) st.push_back(nums[i]); 11 } 12 13 return st; 14 } 15 };