forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryKthSmallestTrimmedNumber.java
More file actions
32 lines (26 loc) · 1.19 KB
/
QueryKthSmallestTrimmedNumber.java
File metadata and controls
32 lines (26 loc) · 1.19 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
// https://leetcode.com/problems/query-kth-smallest-trimmed-number/
// @author: anuj0503
class Solution {
public int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {
int[] result = new int[queries.length];
for(int i = 0; i < queries.length; i++){
PriorityQueue<Pair<String, Integer>> pq = new PriorityQueue<>(new Comparator<Pair<String, Integer>>(){
@Override
public int compare(Pair<String, Integer> p1, Pair<String, Integer> p2){
if(p1.getKey().equals(p2.getKey())){
return p2.getValue() - p1.getValue();
}
return p2.getKey().compareTo(p1.getKey());
}
});
for(int j = 0; j < nums.length; j++){
pq.offer(new Pair<String, Integer>(nums[j].substring(nums[j].length() - queries[i][1]), j));
if(pq.size() == queries[i][0] + 1){
pq.poll();
}
}
result[i] = pq.poll().getValue();
}
return result;
}
}