Muscardinus

순위검색 본문

알고리즘 문제/[프로그래머스] Lv2

순위검색

Muscardinus 2022. 2. 2. 21:02
728x90

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

function solution(info, query) {
    const answer = [];
    const infoMap = {};
    
    // info를 obj에 저장해서 처리
    
    const combination = (array, score, start) => {
        const key = array.join("");
        if (infoMap[key]) infoMap[key].push(score);
        else infoMap[key] = [score];
        for (let i = start; i < array.length; i++) {
            const temp = [...array];
            temp[i] = "-";
            combination(temp, score, i + 1);
        }
    }
    
    for (const e of info) {
        const splited = e.split(" ");
        const score = +splited.pop();
        combination(splited, score, 0);
    }
    
    for (const key in infoMap) {
        infoMap[key] = infoMap[key].sort((a, b) => a - b);
    }
    
    for (const e of query) {
        const splitted = e.replace(/ and /g, " ").split(" ");
        const score = +splitted.pop();
        const key = splitted.join("");
        const arr = infoMap[key];
        if (arr) {
            let start = 0;
            let end = arr.length;
            while (start < end) {
                const mid = Math.floor((start + end) / 2);
                if (arr[mid] >= score) end = mid;
                else if (arr[mid] < score) start = mid + 1;
            }
            const result = arr.length - start;
            answer.push(result);
        } else answer.push(0);
    }
    return answer;
}
728x90

'알고리즘 문제 > [프로그래머스] Lv2' 카테고리의 다른 글

게임 맵 최단거리  (0) 2022.03.28
양궁대회  (0) 2022.03.27
거리두기 확인하기  (0) 2022.01.28
행렬 테두리 회전하기  (0) 2022.01.23
[3차] 방금그곡  (0) 2021.02.10
Comments