-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1985.java
More file actions
44 lines (38 loc) · 1.4 KB
/
_1985.java
File metadata and controls
44 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.github.aditya;
import java.math.BigInteger;
import java.util.PriorityQueue;
public class _1985 {
// 66 ms, faster than 60.66%, 64.8 MB, less than 61.93%
class Solution {
public String kthLargestNumber(String[] nums, int k) {
PriorityQueue<String> minHeap = new PriorityQueue<>((s1, s2) -> {
if (s1.length() == s2.length()) return s1.compareTo(s2);
else return s1.length() - s2.length();
});
for (int i = 0; i < nums.length; i++) {
if (i < k) minHeap.add(nums[i]);
else {
minHeap.add(nums[i]);
minHeap.poll();
}
}
return minHeap.peek();
}
}
// 159 ms, faster than 23.59%, Using BigInteger and String parsing, Not optimal
class Solution_1 {
public String kthLargestNumber(String[] nums, int k) {
PriorityQueue<BigInteger> minHeap = new PriorityQueue<>();
for (int i = 0; i < k; i++) {
minHeap.add(new BigInteger(nums[i]));
}
for (int i = k; i < nums.length; i++) {
if (minHeap.peek().compareTo(new BigInteger(nums[i])) > -1) {
minHeap.poll();
minHeap.add(new BigInteger(nums[i]));
}
}
return minHeap.peek().toString();
}
}
}