import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Solution {
public int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
Map<String, List<Integer>> map = makeIndex(info);
for(int i=0; i< query.length; i++){
int index = query[i].lastIndexOf(" ");
String key = query[i].substring(0, index);
int score = Integer.parseInt(query[i].substring(index + 1));
answer[i] = count(map.get(key), score);
}
return answer;
}
private int count(List<Integer> list, int score) {
if(list == null) return 0;
if(list.isEmpty()) return 0;
int left = 0;
int right = list.size() - 1;
while(left <= right){
int mid = (left + right) / 2;
if(list.get(mid) < score) left = mid + 1;
else right = mid - 1;
}
return list.size() - left;
}
private Map<String, List<Integer>> makeIndex(String[] info) {
Map<String, List<Integer>> map = new HashMap<>();
for(String i : info){
String[] bits = i.split(" ");
String[] langs = {"-", bits[0]};
String[] jobs = {"-", bits[1]};
String[] careers = {"-", bits[2]};
String[] foods = {"-", bits[3]};
int score = Integer.parseInt(bits[4]);
for(String lang : langs){
for(String job : jobs){
for(String career : careers){
for(String food : foods){
String key = lang + " and " + job + " and " + career + " and " + food;
map.putIfAbsent(key, new ArrayList<Integer>());
map.get(key).add(score);
}
}
}
}
}
map.forEach((key, scores) -> scores.sort(Integer::compareTo));
return map;
}
}
'Coding Test > 프로그래머스' 카테고리의 다른 글
[프로그래머스/자바] 튜플 (0) | 2023.05.12 |
---|---|
[프로그래머스/자바] 타겟 넘버 (0) | 2023.05.11 |
[프로그래머스/자바] 특정한 문자를 대문자로 바꾸기 (0) | 2023.05.09 |
[프로그래머스/자바] 대문자와 소문자 (0) | 2023.05.09 |
[프로그래머스/자바] 문자열 바꿔서 찾기 (0) | 2023.05.09 |