프로그래머스_2021 KAKAO BLIND RECRUITMENT

Written on February 14, 2021

2021 카카오 신입공채 1차 온라인 코딩 테스트 문제 차근차근 풀어보기 [참고]

신규 아이디 추천

function replaceDot (str) {
    str = str.replace(/[.]{2,}/g, '.');
    str = str.replace(/^[.]/, '');
    str = str.replace(/[.]$/, ''); 
    return str;
}

function solution(new_id) {
    const regExpId = /[^0-9a-z-_.]/g;
    new_id = new_id.toLowerCase();
    new_id = new_id.replace(regExpId, '');
    new_id = replaceDot(new_id);
    if(new_id.length === 0){
        return solution('a')
    }
    if(new_id.length > 15){
        return solution(new_id.slice(0, 15));
    }
    while(new_id.length < 3){
        new_id += new_id[new_id.length -1]
    }
    return new_id;
}

순위 검색

풀이1 (효율성 실패)

function solution(info, query) {
    let answer = [];
    info = info.map(i => i.split(' '));
    query = query.map(q => q.split(/ and | /));
    query.forEach(q => {
        let count = 0; 
        const [qLan, qEnd, qCarrer, qFood, qScore] = q;
        for(let i = 0; i < info.length; i ++){
            const [lan, end, career, food, score] = info[i];
            if((qLan === '-' || lan === qLan) && (qEnd === '-' || end === qEnd) && (qCarrer === '-' || career === qCarrer) && (qFood === '-' || food === qFood) && (Number(score) >= Number(qScore))) count++;
        }
        answer.push(count);
    })
    return answer;
}


풀이2

function solution(info, query) {
    let answer = [];
    let infoMap = {};
    function genCase(splited, pointer, score){
        const key = splited.join(' ');
        if(infoMap[key]) { infoMap[key].push(score); } else { infoMap[key] = [score]; };
        for(let i = pointer; i < splited.length; i++){
            let temp = [...splited];
            temp[i] = '-';
            genCase(temp, i+1, score);
        };
    };
    
    info = info.map(i => i.split(' '));
    
    for(let i = 0; i < info.length; i++){
        const condition = info[i];
        const score = Number(condition.pop());
        genCase(condition, 0, score);
    };
    for(const key in infoMap){
        infoMap[key] = infoMap[key].sort((a, b) => a - b);
    }

    query = query.map(q => q.split(/ and | /));
    
    for(let i = 0; i < query.length; i++){
        const score = Number(query[i].pop());
        const key = query[i].join(' ');
        const value = infoMap[key];
        if(value){
            let start = 0; 
            let end = value.length; 
            while(start < end){
                const mid = Math.floor((start + end) / 2);
                if(value[mid] < score) start = mid + 1; 
                else end = mid;
            }
            answer.push(value.length - start);
        } else {
            answer.push(0);
        }
    }
    return answer;
}

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself