๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[Javascript/PGS] Lv.3 : ๋ถˆ๋Ÿ‰ ์‚ฌ์šฉ์ž(2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ)

728x90
๋ฐ˜์‘ํ˜•

 

https://school.programmers.co.kr/learn/courses/30/lessons/64064

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 3.

2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ ๊ธฐ์ถœ ๋ฌธ์ œ์ด๋‹ค

์†Œ์š”์‹œ๊ฐ„ 1์‹œ๊ฐ„ ์ด์ƒ ,,,

 

์ฒ˜์Œ์— ๋ดค์„๋• ์‰ฌ์›Œ๋ณด์˜€๋Š”๋ฐ, ๊ฝค ์˜ค๋ž˜ ์‚ฝ์งˆํ–ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋‹ค......

ํ•˜์ง€๋งŒ ์›๋ฆฌ๋งŒ ์ดํ•ดํ•œ๋‹ค๋ฉด ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์„๋“ฏ!!

 

ํ’€์ด ์ˆœ์„œ

 

1. ์ฒซ ์‹œ๋„ ์‹คํŒจ

=> ๋‹จ์ˆœ ๋ฌธ์ž์—ด ๋น„๊ต์ธ ์ค„ ์•Œ๊ณ  ์‚ผ์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์ผ์œผ๋‚˜, ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋Š” ์กฐํ•ฉ ๋ฌธ์ œ์˜€์Œ

 

2. ํžŒํŠธ get

=> dfs๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค๋Š” ํžŒํŠธ๋ฅผ ์–ป์—ˆ๊ณ .... ์•„๋ฌด๋ฆฌ ๋จธ๋ฆฌ๋ฅผ ์จ๋„ ์™œ dfs๋ฅผ ์จ์•ผํ•˜๋Š”๊ฑด์ง€ ์ดํ•ด๊ฐ€ ์•ˆ๋จ

๊ทธ๋ž˜์„œ GPT ๋ถ™์žก๊ณ  ๊ณ„์† ์‚ฝ์งˆํ–ˆ๋‹ค

 

ํžŒํŠธ ๋‚ด๋†”๋ผ~~~~~~~!!!!!

 

3. ์ดํ•ด ์„ฑ๊ณต

=> banned_id[0]์— ๋งค์นญ๋˜๋Š” ํ•œ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•˜์„ ๋•Œ, ๊ทธ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋‹ค์‹œ ํƒ์ƒ‰์„ ํ•ด์•ผํ•˜๊ณ 

       ํ•œ ๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด banned_id[0]์— ๋งค์นญ๋˜๋Š” ๋‹ค๋ฅธ ๊ฒฝ์šฐ์˜ ํƒ์ƒ‰๋„ ์ง„ํ–‰ํ•˜๊ธฐ์—

        ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๊นŠ์ด์šฐ์„  ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ!

=> ๊ทธ๋ฆฌ๊ณ  ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ, Set์€ ๋ฐฐ์—ด์„ ๋น„๊ตํ•  ๋•Œ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ๋‹ค๋ฅธ ์š”์†Œ๋กœ ์ธ์‹ํ•จ.

        ๋”ฐ๋ผ์„œ Set์œผ๋กœ ํ•„ํ„ฐ๋งํ•  ๋•Œ ans_arr์˜ ๋‚ด๋ถ€ ์š”์†Œ๋ฅผ ์ •๋ ฌ ํ›„ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ์ฃผ์—ˆ๋‹ค.

 

 

๋‚˜์˜ ํ’€์ด

let answer = 0;
var ans_arr = [];

const isMatch=(ban, id)=>{
    if(!ban || !id) return false;
    if(ban.length===id.length){
        for(let i=0; i<ban.length; ++i){
            if(ban[i]=="*" || ban[i] === id[i]){
                continue;
            }
            else return false;
        }
        return true;
    }
    return false;
}

const dfs=(ban_list, users, ban_result)=>{
    if(ban_list.length === ban_result.length) {
        // console.log(ban_result)
        ans_arr.push(ban_result);
        return;
    }
    let ban = ban_list[ban_result.length];
    for(let i=0; i<users.length; ++i){
        if(isMatch(ban, users[i])){
            // console.log(ban, users[i]);
            dfs(ban_list, users.filter((_, idx) => idx !== i), 
                    [...ban_result, users[i]]);
        }
    }
}

function solution(user_id, banned_id) {
    let remain_users = [...user_id];
    let ban_list = [...banned_id];
    dfs(ban_list, remain_users, []);
    // console.log(ans_arr);
    answer = Array.from(new Set(ans_arr.map(x=>x.sort().join(',')))).length;
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•