1、时间复杂度:O(NlogK)。
std::vector<int> arr = {10, 3, 5, 6, 2, 3, 112, 35}; std::priority_queue<int, std::vector<int>, std::greater<int> > q; // std::priority_queue<int> q; int K = 3; for (int i = 0; i < arr.size(); i++) { if (i < K) q.push(arr[i]); else { if (arr[i] > q.top()) // if (arr[i] < q.top()) { q.pop(); q.push(arr[i]); } } }