Skip to content

Commit 72c1939

Browse files
authored
Create Solution3.java
1 parent cb93bed commit 72c1939

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int findKthLargest(int[] nums, int k) {
3+
Map<Integer, Integer> cnt = new HashMap<>(nums.length);
4+
int m = Integer.MIN_VALUE;
5+
for (int x : nums) {
6+
m = Math.max(m, x);
7+
cnt.merge(x, 1, Integer::sum);
8+
}
9+
for (int i = m;; --i) {
10+
k -= cnt.getOrDefault(i, 0);
11+
if (k <= 0) {
12+
return i;
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)