class Solution {
static HashMap<String, ArrayList<Integer>> map;
public int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
map = new HashMap<>();
for (String info_content : info) {
String[] content = info_content.split(" ");
div(content, 0, "");
}
int queryIdx=0;
for (String query_content : query) {
String str = query_content.replace(" and ", "");
String[] content = str.split(" ");
Collections.sort(map.get(content[0]));
answer[queryIdx++] = binarySearch(content[0], Integer.parseInt(content[1]));
}
return answer;
}
public void div (String[] content, int idx, String str){
if(idx==4){
if(map.containsKey(str)) {map.get(str).add(Integer.parseInt(content[content.length - 1]));}
else{ArrayList<Integer> arr = new ArrayList<>();
arr.add(Integer.parseInt(content[content.length - 1]));
map.put(str,arr);}
return;
}
div(content, idx + 1, str+"-");
div(content, idx + 1, str + content[idx]);
}
public int binarySearch(String key, int hope_score) {
if (!map.containsKey(key)) {
return 0;
}
ArrayList<Integer> arr = new ArrayList<>();
arr = map.get(key);
int start=0;
int end = arr.size()-1;
int mid=0;
while (start <= end) {
mid = (start+end)/2;
if (hope_score > arr.get(mid)) {
start = mid + 1;
} else {
end = mid-1;
}
}
return arr.size() - start;
}
}
https://school.programmers.co.kr/learn/courses/30/lessons/72412?language=java#
이진 탐색에 관한 문제였다.
완전한 답은 아니어서 ㅜ 다시 공부 해봐야 할 것 같다,,