Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it without sorting?
Example 1:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Example 2:
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
A min heap in C++ can be implemented using various approaches, but the most common and easiest way is to use the priority queue from the Standard Template Library (STL). By default, std::priority_queue is a max heap, but you can transform it into a min heap by using a custom comparator.
Here’s a step-by-step guide on how to use a min heap in C++ using std::priority_queue:
You need to include the <queue> and <vector> headers for using priority_queue.
#include <iostream>
#include <queue>
#include <vector>
By default, std::priority_queue is a max heap, so you need to use greater<T> as the comparator to create a min heap.
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
int defines the data type.std::vector<int> specifies that you will store the elements in a vector.std::greater<int> defines that it should act as a min heap.You can use the push function to insert elements into the heap.
minHeap.push(10);
minHeap.push(30);
minHeap.push(20);
minHeap.push(5);
The minimum element is always at the top of the heap. You can access it using the top function.
std::cout << "Min element: " << minHeap.top() << std::endl; // Output will be 5
You can remove the top (minimum) element using the pop function.
minHeap.pop(); // Removes the element 5
You can check if the heap is empty using the empty function.
if (minHeap.empty()) {
std::cout << "The heap is empty." << std::endl;
}
To get the number of elements in the heap, use the size function.
std::cout << "Size of heap: " << minHeap.size() << std::endl;
#include <iostream>
#include <queue>
#include <vector>
int main() {
// Define a min heap
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
// Push elements into the heap
minHeap.push(10);
minHeap.push(30);
minHeap.push(20);
minHeap.push(5);
// Access and remove elements
std::cout << "Min element: " << minHeap.top() << std::endl; // Output: 5
minHeap.pop(); // Removes the element 5
std::cout << "Next min element: " << minHeap.top() << std::endl; // Output: 10
return 0;
}
push().top().pop().empty() to check if the heap is empty.size() to check how many elements are in the heap.This should help you use a min heap in C++ effectively! Let me know if you need more details.
class Solution
{
public:
int findKthLargest(vector<int> &nums, int k)
{
priority_queue<int, vector<int>, greater<int>> karr;
for (int i = 0; i < k; i++)
{
karr.push(nums.at(i));
}
for (int i = k; i < nums.size(); i++)
{
if (nums.at(i) > karr.top())
{
karr.pop();
karr.push(nums.at(i));
}
}
return karr.top();
}
};
62/62 cases passed (10 ms)
Your runtime beats 99.59 % of cpp submissions
Your memory usage beats 90.68 % of cpp submissions (12 MB)
Your solution is already quite efficient and follows the correct approach using a min heap to find the k-th largest element. To make it more "C++-style" and improve readability, there are a few minor adjustments you can consider:
Range-based for loops make your code more readable and are considered more modern in C++.
emplace Instead of push:The emplace() method constructs the element directly in the container, which can be slightly more efficient than push().
at() Method in Favor of Direct Access:Since you are certain about the bounds, accessing elements with nums[i] is more idiomatic and doesn't incur the overhead of boundary checking.
Passing the input vector as a const reference ensures that the function won't modify the input and avoids unnecessary copying.
Here’s the improved version of your solution:
class Solution
{
public:
int findKthLargest(const std::vector<int> &nums, int k)
{
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
// Insert the first k elements into the min heap
for (int i = 0; i < k; ++i)
{
minHeap.emplace(nums[i]);
}
// Process the remaining elements
for (int i = k; i < nums.size(); ++i)
{
if (nums[i] > minHeap.top())
{
minHeap.pop();
minHeap.emplace(nums[i]);
}
}
return minHeap.top();
}
};
emplace(): It is used to construct elements in-place in the heap.nums[i] is used instead of nums.at(i) since it’s more efficient and appropriate here.const reference: Passing the vector as a constant reference ensures that the function doesn’t modify the input and improves performance.These small improvements make the code more idiomatic and efficient in C++ while maintaining the same functionality.