Lv2. 순위 검색 Javascript
https://programmers.co.kr/learn/courses/30/lessons/72412
function solution(info, query) {
const answer = [];
let map = {};
function combination(infos, score, map, start) {
let key = infos.join("");
let value = map[key];
if (value) {
map[key].push(score);
} else {
map[key] = [score];
}
for (let i = start; i < infos.length; i++) {
let combiArr = [...infos];
combiArr[i] = '-';
combination(combiArr, score, map, i + 1);
}
}
function binarySearch(map, key, score) {
let scoreArr = map[key];
if (scoreArr) {
let start = 0;
let end = scoreArr.length;
while (start < end) {
let mid = Math.floor((start + end) / 2);
if (scoreArr[mid] >= score) {
end = mid;
} else if (scoreArr[mid] < score) {
start = mid + 1;
}
}
return scoreArr.length - start;
}
else return 0
}
for (let i = 0; i < info.length; i++) {
const infos = info[i].split(" ");
const score = infos.pop();
combination(infos, score, map, 0);
}
for (let key in map) {
map[key].sort((o1, o2) => o1 - o2);
}
for (let i = 0; i < query.length; i++) {
let queries = query[i].replace(/ and /g, "").split(" ");
let score = Number(queries.pop());
answer.push(binarySearch(map, queries[0], score));
}
return answer;
}
공간을 내어주고 시간을 취하는 방식
2021 카카오 신입공채 1차 온라인 코딩 테스트 for Tech developers 문제해설
[프로그래머스 2021 KAKAO BLIND RECRUITMENT] 순위 검색 - JavaScript
댓글 환영
질문 환영
by.protect-me