|
| 1 | +--- |
| 2 | +id: kth-largest-element-in-an-array |
| 3 | +title: Kth Largest Element in an Array (LeetCode) |
| 4 | +sidebar_label: 0215-KthLargestElementInAnArray |
| 5 | +description: Find the kth largest element in an unsorted array. Note that it is the kth largest element in sorted order, not the kth distinct element. |
| 6 | +--- |
| 7 | + |
| 8 | +## Problem Description |
| 9 | + |
| 10 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 11 | +| :---------------- | :------------ | :--------------- | |
| 12 | +| [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Kth Largest Element in an Array Solution on LeetCode](https://leetcode.com/problems/kth-largest-element-in-an-array/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given an integer array `nums` and an integer `k`, return the `k`th largest element in the array. |
| 17 | + |
| 18 | +Note that it is the `k`th largest element in sorted order, not the `k`th distinct element. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +#### Example 1 |
| 23 | + |
| 24 | +- **Input:** `nums = [3,2,1,5,6,4]`, `k = 2` |
| 25 | +- **Output:** `5` |
| 26 | +- **Explanation:** The second largest element is 5. |
| 27 | + |
| 28 | +#### Example 2 |
| 29 | + |
| 30 | +- **Input:** `nums = [3,2,3,1,2,4,5,5,6]`, `k = 4` |
| 31 | +- **Output:** `4` |
| 32 | +- **Explanation:** The fourth largest element is 4. |
| 33 | + |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- `1 <= k <= nums.length <= 10^4` |
| 37 | +- $-10^4 <= nums[i] <= 10^4$ |
| 38 | + |
| 39 | +### Approach |
| 40 | + |
| 41 | +To find the `k`th largest element in an unsorted array, we can use various methods such as sorting, using a min-heap, or using Quickselect algorithm. Here are the approaches: |
| 42 | + |
| 43 | +1. **Sorting**: |
| 44 | + - Sort the array and return the element at index `len(nums) - k`. |
| 45 | + |
| 46 | +2. **Min-Heap**: |
| 47 | + - Maintain a min-heap of size `k`. |
| 48 | + - Iterate through the array and maintain the heap with the largest `k` elements. |
| 49 | + - The root of the heap will be the `k`th largest element. |
| 50 | + |
| 51 | +3. **Quickselect**: |
| 52 | + - Use a partition-based method to find the `k`th largest element in `O(n)` average time complexity. |
| 53 | + |
| 54 | +### Solution Code |
| 55 | + |
| 56 | +#### Python |
| 57 | + |
| 58 | +```python |
| 59 | +import heapq |
| 60 | + |
| 61 | +class Solution: |
| 62 | + def findKthLargest(self, nums, k): |
| 63 | + return heapq.nlargest(k, nums)[-1] |
| 64 | +``` |
| 65 | + |
| 66 | +#### Java |
| 67 | + |
| 68 | +```java |
| 69 | +import java.util.PriorityQueue; |
| 70 | + |
| 71 | +class Solution { |
| 72 | + public int findKthLargest(int[] nums, int k) { |
| 73 | + PriorityQueue<Integer> minHeap = new PriorityQueue<>(); |
| 74 | + for (int num : nums) { |
| 75 | + minHeap.offer(num); |
| 76 | + if (minHeap.size() > k) { |
| 77 | + minHeap.poll(); |
| 78 | + } |
| 79 | + } |
| 80 | + return minHeap.peek(); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### C++ |
| 86 | + |
| 87 | +```c++ |
| 88 | +#include <queue> |
| 89 | +#include <vector> |
| 90 | + |
| 91 | +class Solution { |
| 92 | +public: |
| 93 | + int findKthLargest(vector<int>& nums, int k) { |
| 94 | + priority_queue<int, vector<int>, greater<int>> minHeap; |
| 95 | + for (int num : nums) { |
| 96 | + minHeap.push(num); |
| 97 | + if (minHeap.size() > k) { |
| 98 | + minHeap.pop(); |
| 99 | + } |
| 100 | + } |
| 101 | + return minHeap.top(); |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | +
|
| 106 | +### Conclusion |
| 107 | +The above solutions effectively find the kth largest element in an array using different methods. |
| 108 | +The min-heap approach provides an efficient solution with a time complexity of O(n log k), making it |
| 109 | +suitable for larger inputs while ensuring the correct element is found. Each implementation handles |
| 110 | +edge cases and constraints effectively, providing robust solutions across various programming |
| 111 | +languages. |
0 commit comments