cpp
class Solution {
public:
vector<int> inventoryManagement(vector<int>& stock, int cnt) {
//快排
int left=0;
int right=stock.size()-1;
cnt-=1;
int flag;
while(left<=right){
flag=patition(left,right,stock);
if(flag==cnt){
vector<int> tmp;
for(int i=0;i<=flag;i++)
tmp.push_back(stock[i]);
return tmp;
}
else if(flag>cnt)
right=flag-1;
else
left=flag+1;
}
vector<int> n1;
return n1;
}
int patition(int left,int right,vector<int>& stock){
int tmp=stock[left];
int i=left+1;
int j=right;
while(i<=j){
while(i<right && stock[i]<=tmp)
i+=1;
//找交换位置 tmp
while(j>left && stock[j]>=tmp)
j-=1;
if(i>=j)
{
break;
}
swap(stock[i],stock[j]);
}
swap(stock[j],stock[left]);
return j;
}
};
100% 快吗?空间换的hhh