最小K个数

文章目录

  • 题意
  • 思路
  • 代码

题意

题目链接

思路

代码

class Solution {
public:
    vector smallestK(vector& arr, int k) {
        priority_queue Q;
        for (auto &index:arr)
        {
            Q.push(index);
            if (Q.size() > k)
                Q.pop();
        }
        vector ans;
        while (!Q.empty())
        {
            ans.push_back(Q.top());
            Q.pop();
        }
        return ans;
    }
};

你可能感兴趣的:(算法,leetcode,职场和发展)