题目讲解
LCR 159. 库存管理 III
本题的含义就是让求出最小的k个数
算法讲解
class Solution {
public:
void my_qsort(vector<int>& nums, int l, int r)
{
if(l >= r) return ;
int i = l, left = l-1, right = r+1;
int key = nums[rand() % (r - l + 1) + l];
//完成分三路 <key == key >key
while(i < right)
{
if(nums[i] < key) swap(nums[++left], nums[i++]);
else if(nums[i] == key) i++;
else swap(nums[--right], nums[i]);
}
//分治这三路
my_qsort(nums, l, left);
my_qsort(nums, right, r);
}
vector<int> inventoryManagement(vector<int>& arr, int k) {
//分三路-》直接快速排序-》 排升序
srand(time(NULL));
int n = arr.size();
my_qsort(arr, 0, n-1);
return {arr.begin(), arr.begin()+k};
}
};