adding min heap solution

This commit is contained in:
Jidong Xiao
2023-11-20 17:07:06 -05:00
parent 35571ccb83
commit 0e2e2e3904
2 changed files with 20 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
vector<int> result;
priority_queue<int, vector<int>, std::greater<int>> pq;
int size = nums.size();
// build the priority queue, which is now a min heap
for(int i=0;i<size;i++){
pq.push(nums[i]);
}
// pop out the smallest element size times
for(int i=0;i<size;i++){
result.push_back(pq.top());
pq.pop();
}
// now data is sorted in an ascending order
return result;
}
};