Created
October 3, 2021 16:26
-
-
Save ahmedeltaher/3ca9864b2b5cbd44fd1ffa9b038f0ca5 to your computer and use it in GitHub Desktop.
215. Kth Largest Element in an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int findKthLargest(int[] nums, int k) { | |
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(); | |
for(int num : nums){ | |
maxHeap.add(num); | |
if(maxHeap.size()>k){ | |
maxHeap.poll(); | |
} | |
} | |
return maxHeap.poll(); | |
} | |
} | |
// Time ==> O(nlog(k)), n ==> arraysize, k heap size | |
// Sapce ==> O(k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment