Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite55b429

Browse files
Merge pull requestneetcode-gh#1 from GauravVarshikar/GauravVarshikar-Kth-Largest
Create 215-Kth-Largest-Element-in-an-Array.java
2 parents01a2b36 +76a043e commite55b429

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classSolution {
2+
publicintfindKthLargest(int[]nums,intk) {
3+
//create a min heap
4+
PriorityQueue<Integer>heap =newPriorityQueue();
5+
6+
//iterate over the array
7+
for(intn:nums){
8+
//first add the integer to heap
9+
heap.add(n);
10+
//if size of the heap is greater than k
11+
if(heap.size() >k){
12+
//remove the root element (lowest of all)
13+
heap.poll();
14+
}
15+
}
16+
//finally heap has k largest elements left with root as the kth largest element
17+
returnheap.peek();
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp