-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchRank.java
63 lines (48 loc) · 1.78 KB
/
SearchRank.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
static HashMap<String, ArrayList<Integer>> map = new HashMap<>();
public int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
// HashMap에 지원한 사람이 query에 통과할 수 있는 모든 경우의 수를 담아준다 ("-" 조건 때문에)
makeMap(info);
for(String key : map.keySet()) Collections.sort(map.get(key));
for(int i=0; i< query.length; i++){
String[] tmp = query[i].replace(" and ", "").split(" ");
int score = Integer.parseInt(tmp[1]);
answer[i] = binarySearch(tmp[0], score);
}
return answer;
}
private int binarySearch(String key, int score){
if(map.containsKey(key)){
ArrayList<Integer> list = map.get(key);
int left = 0;
int right = list.size()-1;
if(list.get(right) < score) return 0;
while(left < right){
int mid = (left + right) / 2;
if(list.get(mid) < score) left = mid +1;
else right = mid;
}
return list.size() - left;
}
return 0;
}
private void makeMap(String[] info){
for(int i=0; i<info.length; i++){
dfs("" , info[i].split(" "), 0);
}
}
private void dfs(String str, String [] info, int depth){
if(depth == 4) {
if (!map.containsKey(str)) {
ArrayList<Integer> list = new ArrayList<>();
list.add(Integer.parseInt(info[4]));
map.put(str, list);
} else
map.get(str).add(Integer.parseInt(info[4]));
return;
}
dfs(str+"-",info, depth+1);
dfs(str+info[depth],info, depth+1);
}
}