Skip to content

Commit

Permalink
impl kth largest element in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Mar 14, 2024
1 parent 1d637a7 commit 96e32ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions leetcode-cc/Heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ template <typename T>
T Heap<T>::top() {
return inner[0]; // panic if inner is empty
}

template class Heap<int>;
16 changes: 15 additions & 1 deletion leetcode-cc/KthLargestElement.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Heap.h"
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"
Expand Down Expand Up @@ -27,5 +28,18 @@ class SKthLargestElement : public ISolution {
int benchmark() const override { return 0; }

private:
int findKthLargest(vector<int>& nums, int k) const {}
int findKthLargest(vector<int>& nums, int k) const {
Heap<int> heap;

for (const int& num : nums) {
heap.insert(num);
}

while (k > 1) {
heap.pop();
k--;
}

return heap.top();
}
};
5 changes: 5 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -770,5 +770,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SBTRightSideView>(); });

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PKthLargestElement>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SKthLargestElement>(); });

return 0;
}

0 comments on commit 96e32ec

Please sign in to comment.