Skip to content

Commit 943b3c7

Browse files
authored
Create KthLargest.java
You can solve this question by sorting the array but It Will Take O(nlogn) time however I have done it using min heap Time Complexity: O(nlogk) We should optimize our code as efficient as possible
1 parent cc29446 commit 943b3c7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

KthLargest.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Find The kth Largest In The Array
2+
//Using Min Heap
3+
4+
public int findKthLargest(int[] nums, int k) {
5+
PriorityQueue<Integer> queue = new PriorityQueue<>();
6+
for (int e :nums){
7+
queue.add(e);
8+
if (queue.size()>k){
9+
queue.poll();
10+
}
11+
12+
}
13+
return queue.peek();
14+
}

0 commit comments

Comments
 (0)