Go๋ฏผ๋ณด๋‹ค Go

ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž

Create

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋‚˜ ๊ทธ๋ž˜๋„ ํ’€๊ธด ํ’€์—ˆ์–ด...?

SleepingOff 2024. 5. 30. 09:42

๐Ÿพ๋“ค์–ด๊ฐ€๋ฉฐ

- ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ > 2024 KAKAO WINTER INTERNSHIP > ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›์€ ์„ ๋ฌผ

๋ฌธ์ œ: ์ด๋ฒˆ ๋‹ฌ์˜ ์„ ๋ฌผ ๊ธฐ๋ก์„ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ๋ˆ„๊ฐ€ ๋งŽ์ด ๋ฐ›์„์ง€ ์˜ˆ์ธกํ•˜๊ธฐ

๋”๋ณด๊ธฐ

์กฐ๊ฑด

  • ๋‘ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์ด ์žˆ๋‹ค๋ฉด, ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ๋‘ ์‚ฌ๋žŒ ์‚ฌ์ด์— ๋” ๋งŽ์€ ์„ ๋ฌผ์„ ์ค€ ์‚ฌ๋žŒ์ด ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ๋‘ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์€ ๊ธฐ๋ก์ด ํ•˜๋‚˜๋„ ์—†๊ฑฐ๋‚˜ ์ฃผ๊ณ ๋ฐ›์€ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด, ์„ ๋ฌผ ์ง€์ˆ˜๊ฐ€ ๋” ํฐ ์‚ฌ๋žŒ์ด ์„ ๋ฌผ ์ง€์ˆ˜๊ฐ€ ๋” ์ž‘์€ ์‚ฌ๋žŒ์—๊ฒŒ ์„ ๋ฌผ์„ ํ•˜๋‚˜ ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์„ ๋ฌผ ์ง€์ˆ˜๋Š” ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ์ž์‹ ์ด ์นœ๊ตฌ๋“ค์—๊ฒŒ ์ค€ ์„ ๋ฌผ์˜ ์ˆ˜์—์„œ ๋ฐ›์€ ์„ ๋ฌผ์˜ ์ˆ˜๋ฅผ ๋บ€ ๊ฐ’์ž…๋‹ˆ๋‹ค.
    • ๋งŒ์•ฝ ๋‘ ์‚ฌ๋žŒ์˜ ์„ ๋ฌผ ์ง€์ˆ˜๋„ ๊ฐ™๋‹ค๋ฉด ๋‹ค์Œ ๋‹ฌ์— ์„ ๋ฌผ์„ ์ฃผ๊ณ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…๋ ฅ

  • ์นœ๊ตฌ๋“ค์˜ ์ด๋ฆ„์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด friends
  • ์ด๋ฒˆ ๋‹ฌ๊นŒ์ง€ ์นœ๊ตฌ๋“ค์ด ์ฃผ๊ณ ๋ฐ›์€ ์„ ๋ฌผ ๊ธฐ๋ก์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด gifts

์ถœ๋ ฅ

  • ๋‹ค์Œ๋‹ฌ์— ๊ฐ€์žฅ ๋งŽ์€ ์„ ๋ฌผ์„ ๋ฐ›๋Š” ์นœ๊ตฌ๊ฐ€ ๋ฐ›์„ ์„ ๋ฌผ์˜ ์ˆ˜

 

โœจ๋ณธ๊ฒฉ์ ์œผ๋กœ

๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ

function solution(friends, gifts) {
    const graph = {};
    const index= {};
    const next = {};
    
    //init graph
    friends.forEach((f,i)=>{
        graph[f] = {}
        friends.forEach((fi,i)=>{
            if(fi != f) graph[f][fi] = gifts.filter(ig => (ig.split(" ")[0] === f) && fi === ig.split(" ")[1]).length;
        })
    })
    //distance
    friends.forEach((f,i)=>{
        next[f] = 0;
        gifts.forEach((gift, k)=>{
            const g = gift.split(" ");
            const receive = gifts.filter(ig => ig.split(" ")[0] === f).length;
            const give = gifts.filter(ig => ig.split(" ")[1] === f).length;
            index[f] = receive - give;
        })
    })
    //next month
    friends.forEach((f, i)=>{
        friends.forEach((fi, ii)=>{
            const g = graph[f][fi];
            const r = graph[fi][f];
            
            if((!!g || !!r) && g > r) next[f]++;
            if(g === r && index[f] > index[fi]) next[f]++;
            
        })
    })
    
    const value = Object.values(next);
    return Math.max(...value);
}

๊ฒฐ๊ณผ: ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผ... ์‹คํŒจ๋Š” ๋ชจ๋‘ `์‹œ๊ฐ„ ์ดˆ๊ณผ`

์‹คํ–‰ ๊ฒฐ๊ณผ

์Šค์Šค๋กœ ์ƒ๊ฐํ•œ ์ด์œ ๋Š” ์ค‘์ฒฉ ๋ฐ˜๋ณต์„ ๋ฌธ์ œ๋กœ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.  ์‹ฌ์ง€์–ด ๊ทธ ์•ˆ์—์„œ filter๊นŒ์ง€ ์“ฐ๊ณ  ์žˆ์–ด์„œ 3๋ฒˆ ์ค‘์ฒฉ์ด๋ผ๊ณ  ๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ ... ChatGPT์•ผ ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด์ค˜.

  1. ์„ ๋ฌผ ์ฃผ๊ณ ๋ฐ›์€ ํšŸ์ˆ˜ ๊ณ„์‚ฐ:
    • gifts ๋ฐฐ์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ˆœํšŒํ•˜์—ฌ filter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ๋‹จ์ผ ์ˆœํšŒ๋กœ ์„ ๋ฌผ ์ฃผ๊ณ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.
  2. ์„ ๋ฌผ ์ง€์ˆ˜ ๊ณ„์‚ฐ:
    • ์„ ๋ฌผ ์ฃผ๊ณ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋†“์œผ๋ฉด, ์„ ๋ฌผ ์ง€์ˆ˜๋„ ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ๋‹ค์Œ ๋‹ฌ ์„ ๋ฌผ ๋ฐ›์„ ํšŸ์ˆ˜ ๊ณ„์‚ฐ:
    • ์„ ๋ฌผ ์ฃผ๊ณ ๋ฐ›์€ ํšŸ์ˆ˜์™€ ์„ ๋ฌผ ์ง€์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋‘๋ฉด, ์ด ๊ฐ’์„ ์ด์šฉํ•ด ํ•œ ๋ฒˆ์— ๋น„๊ตํ•˜๊ณ  ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•—.. ์ตœ์ ํ™”ํ•œ ์ฝ”๋“œ๊นŒ์ง€ ๋‚˜์™€ ๋ณด๊ณ  ๋ง์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๋ถ€ํ„ด ํฌ์ธํŠธ๋งŒ ๋ฐ›์•„์„œ ์ง์ ‘ ๊ณ ์ณ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € `graph`์ž…๋‹ˆ๋‹ค.

    //init graph
    friends.forEach((f,i)=>{
        graph[f] = {}
        friends.forEach((fi,i)=>{
            if(fi != f) graph[f][fi] = gifts.filter(ig => (ig.split(" ")[0] === f) && fi === ig.split(" ")[1]).length;
        })
    })

์ฒ˜์Œ์— ์‚ฌ์‹ค ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์ง€ ์•Š์•„์„œ ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ์— ๋Œ€ํ•œ ์„ค๋ช…์—์„œ ํ‘œ์ฒ˜๋Ÿผ ๋‚˜์˜จ ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ๊ทธ๋ ค๋ณด๋ ค๊ณ  ํ–ˆ๋˜ ๊ฒƒ์ด ํฐ ํ ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์‹ค์งˆ์ ์œผ๋กœ ๊ทธ๋ž˜ํ”„๋Š” ๋งˆ์ง€๋ง‰์— ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋ฟ์ธ๋ฐ ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„์„ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฐ ํ• ์• ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ์‹ค์งˆ์ ์œผ๋กœ ์ค‘์š”ํ•œ ์š”์†Œ๋Š” `๋ˆ„๊ตฌ`๊ฐ€ ์•„๋‹Œ `ํšŸ์ˆ˜`์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰graph ์‚ญ์ œ!

๋”ฐ๋ผ์„œ `index`์ž…๋‹ˆ๋‹ค.

    //distance
    friends.forEach((f,i)=>{
        next[f] = 0;
        gifts.forEach((gift, k)=>{
            const g = gift.split(" ");
            const receive = gifts.filter(ig => ig.split(" ")[0] === f).length;
            const give = gifts.filter(ig => ig.split(" ")[1] === f).length;
            index[f] = receive - give;
        })
    })

์—ฌ๊ธฐ์—์„œ๋„ 3์ค‘์ฒฉ์ž…๋‹ˆ๋‹ค... ๋ฐ”๋กœ๋ฐ”๋กœ receive์™€ give์˜ ํšŸ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด 2๋ฒˆ์งธ ์ค‘์ฒฉ์€ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

    //distance
    friends.forEach((f,i)=>{
        next[f] = 0;
        const receive = gifts.filter(ig => ig.split(" ")[0] === f).length;
        const give = gifts.filter(ig => ig.split(" ")[1] === f).length;
        index[f] = receive - give;
    })

๊ทธ๋ž˜๋„ ์•„์ง ์ค‘์ฒฉ ๋ฐ˜๋ณต์ž…๋‹ˆ๋‹ค. filter๋ฅผ ํ™•์‹คํžˆ ์—†์• ๊ธฐ ์œ„ํ•ด receive์™€ give๋ฅผ ๋ณ„๋„๋กœ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„์€ ๋ณ„๋„๋กœ ๋นผ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ `receive`, `give` ๋ณ„๋„ ์„ ์–ธ, ์ดˆ๊ธฐํ™” ๋ถ„๋ฆฌ

    const index= {};
    const next = {};
    const receive = {};
    const give = {};
    
    //init graph
    friends.forEach((f,i)=>{
        next[f] = 0;
        receive[f] = 0;
        give[f] = 0;
    })
   
    //distance
    gifts.forEach((gift, k)=>{
        const g = gift.split(" ");
        receive[g[1]]++;
        give[g[0]]++;
    })

์ค‘์ฒฉ ๋ฐ˜๋ณต์ด ์‚ฌ๋ผ์กŒ๋‹ค...!

๊ทธ๋ฆฌ๊ณ  `graph`๋ฅผ ๋Œ€์ฒดํ•ด์ค๋‹ˆ๋‹ค.

    //next month
    friends.forEach((f, i)=>{
        friends.forEach((fi, ii)=>{
            const g = graph[f][fi];
            const r = graph[fi][f];
            
            if((!!g || !!r) && g > r) next[f]++;
            if(g === r && index[f] > index[fi]) next[f]++;
            
        })
    })

๊ธฐ์กด์— ๊ทธ๋ž˜ํ”„๊ฐ€ ์“ฐ์ธ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ GPT์˜ ์ฝ”๋“œ๋ฅผ ๋นŒ๋ ค์™”์Šต๋‹ˆ๋‹ค(์‚ฌ์‹ค ์ „์ฒด ๋‹ค ๋นŒ๋ ค์™”๋‹ค..) ๋ฐ”๋กœ filter๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พธ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ณ„์† split์„ ์‚ฌ์šฉํ•ด์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฒƒ์„ ๋ฐ”๊ฟ”์•ผ์ง€๋งŒ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ ๊ทธ๋Œ€๋กœ๋ฅผ ๋น„๊ตํ•˜๋ ค๋Š” ์ƒ๊ฐ์€ ๋ชปํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์ž…๋ ฅ๋ฐ›์€ ๋Œ€๋กœ ๋น„๊ต

    //next month
    friends.forEach((f, i)=>{
        friends.forEach((fi, ii)=>{
            const g = gifts.filter(gift => gift === `${f} ${fi}`).length;
            const r = gifts.filter(gift => gift === `${fi} ${f}`).length;
            
            if((g || r) && g > r) next[f]++;
            if(g === r && index[f] > index[fi]) next[f]++;
            
        })
    })

โœ”๏ธ๊ฒฐ๋ก 

์˜ค๋žœ๋งŒ์— ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ํ’€๋‹ค๋ณด๋‹ˆ ๋ฌธ์ œ ์ดํ•ด๋ถ€ํ„ฐ ๋‚œ์ด๋„๊ฐ€ ์žˆ์—ˆ๊ณ , ์ •ํ™•ํžˆ ์–ด๋–ค ๊ฒƒ์„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜๋Š”์ง€๋„ ์• ๋จน์—ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋Š” ๊พธ์ค€ํžˆ ํ’€์–ด์•ผ ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘์ฒฉ ๋ฐ˜๋ณต์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ์ง€ ๊ณ„์† ์˜์‹ฌํ•ด๋ณด๊ธฐ๋„ ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

function solution(friends, gifts) {
    const index= {};
    const next = {};
    const receive = {};
    const give = {};
    
    //init graph
    friends.forEach((f,i)=>{
        next[f] = 0;
        receive[f] = 0;
        give[f] = 0;
    })
   
    //distance
    gifts.forEach((gift, k)=>{
        const g = gift.split(" ");
        receive[g[1]]++;
        give[g[0]]++;
    })
    
    friends.forEach(f => {
        index[f] = give[f] - receive[f];
    });
    
    //next month
    friends.forEach((f, i)=>{
        friends.forEach((fi, ii)=>{
            const g = gifts.filter(gift => gift === `${f} ${fi}`).length;
            const r = gifts.filter(gift => gift === `${fi} ${f}`).length;
            
            if((g || r) && g > r) next[f]++;
            if(g === r && index[f] > index[fi]) next[f]++;
            
        })
    })
    
    const value = Object.values(next);
    return Math.max(...value);
}

 

728x90