공부
[programers] 순위검색 (dfs & binary search)
승가비
2021. 12. 26. 22:49
728x90
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
static Map<String, ArrayList<Integer>> map = new HashMap<>();
public int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
for (int i = 0; i < info.length; i++) {
String[] arr = info[i].split(" ");
dfs(arr.length - 1, "", 0, arr);
}
for (String key : map.keySet()) {
List<Integer> score = map.get(key);
Collections.sort(score);
}
for (int i = 0; i < query.length; i++) {
query[i] = query[i].replaceAll(" and ", "");
String[] arr = query[i].split(" ");
int score = Integer.parseInt(arr[1]);
answer[i] = search(arr[0], score);
}
return answer;
}
static void dfs(int lastIndex, String pos, int depth, String[] info) {
if (depth >= lastIndex) {
String score = info[lastIndex];
if (!map.containsKey(pos)) {
map.put(pos, new ArrayList<>());
}
map.get(pos).add(Integer.parseInt(score));
return;
}
dfs(lastIndex, pos + "-", depth + 1, info);
dfs(lastIndex, pos + info[depth], depth + 1, info);
}
static int search(String str, int score) {
if (!map.containsKey(str)) {
return 0;
}
List<Integer> scoreList = map.get(str);
int start = 0, end = scoreList.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (scoreList.get(mid) < score) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return scoreList.size() - start;
}
}
https://programmers.co.kr/learn/courses/30/lessons/72412
코딩테스트 연습 - 순위 검색
["java backend junior pizza 150","python frontend senior chicken 210","python frontend senior chicken 150","cpp backend senior pizza 260","java backend junior chicken 80","python backend senior chicken 50"] ["java and backend and junior and pizza 100","pyt
programmers.co.kr
https://loosie.tistory.com/265
[프로그래머스] 2021 카카오 / 순위 검색 (Java)
#3 순위 검색 2021 카카오 블라인드 난이도 : LEVEL 2 유형 : 조합, Map, 이진탐색 코딩테스트 연습 - 순위 검색 ["java backend junior pizza 150","python frontend senior chicken 210","python frontend senio..
loosie.tistory.com
728x90