๐Ÿ–ค Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ (Javascript)

์˜ˆ์ง„-D 2023. 5. 8. 12:08

https://school.programmers.co.kr/learn/courses/30/lessons/178871?language=javascript 

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋‚œ์ด๋„๋Š” ๋ ˆ๋ฒจ 1์—์„œ๋„ ๋‚ฎ์€ ํŽธ์ด์ง€๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ง€์‹์ด ๋ฐ”๋‹ฅ์— ๊นŒ๊นŒ์› ๋˜ ์ €๋Š” ์–ป์€ ์ง€์‹์ด ์žˆ์–ด ๊ธฐ๋กํ•˜๊ณ ์ž ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.๐Ÿ’ก

 

 

๋ฌธ์ œ

์–€์—์„œ๋Š” ๋งค๋…„ ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ๊ฐ€ ์—ด๋ฆฝ๋‹ˆ๋‹ค. ํ•ด์„ค์ง„๋“ค์€ ์„ ์ˆ˜๋“ค์ด ์ž๊ธฐ ๋ฐ”๋กœ ์•ž์˜ ์„ ์ˆ˜๋ฅผ ์ถ”์›”ํ•  ๋•Œ ์ถ”์›”ํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1๋“ฑ๋ถ€ํ„ฐ 3๋“ฑ๊นŒ์ง€ "mumu", "soe", "poe" ์„ ์ˆ˜๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ๋‹ฌ๋ฆฌ๊ณ  ์žˆ์„ ๋•Œ, ํ•ด์„ค์ง„์ด "soe"์„ ์ˆ˜๋ฅผ ๋ถˆ๋ €๋‹ค๋ฉด 2๋“ฑ์ธ "soe" ์„ ์ˆ˜๊ฐ€ 1๋“ฑ์ธ "mumu" ์„ ์ˆ˜๋ฅผ ์ถ”์›”ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰ "soe" ์„ ์ˆ˜๊ฐ€ 1๋“ฑ, "mumu" ์„ ์ˆ˜๊ฐ€ 2๋“ฑ์œผ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค.

์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด 1๋“ฑ๋ถ€ํ„ฐ ํ˜„์žฌ ๋“ฑ์ˆ˜ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด players์™€ ํ•ด์„ค์ง„์ด ๋ถ€๋ฅธ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด callings๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฒฝ์ฃผ๊ฐ€ ๋๋‚ฌ์„ ๋•Œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์„ 1๋“ฑ๋ถ€ํ„ฐ ๋“ฑ์ˆ˜ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

ํ’€์ด (์‹œ๊ฐ„ ์ดˆ๊ณผ)

์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ forEach๋ฌธ๊ณผ indexOf๋ฅผ ์‚ฌ์šฉํ•ด index๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ž˜ ํ†ต๊ณผํ–ˆ์ง€๋งŒ, ์ œ์ถœ ํ›„ 4~5๊ฐ€์ง€ ์ผ€์ด์Šค์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์–ด์š”. for๋ฌธ์„ ์•ˆ์“ธ ์ˆœ ์—†์œผ๋‹ˆ indexOf์˜ ๋ฌธ์ œ๊ฐ™์•„ ๊ตฌ๊ธ€์˜ ๋„์›€์„ ๋นŒ๋ ธ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜‚

์ƒ๊ฐํ•ด๋ณด๋‹ˆ indexOf๋„ ๋ฐฐ์—ด์„ ๋Œ๋ฉฐ ํ•ด๋‹น ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•ด์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฝค ์˜ฌ๋ผ๊ฐ€๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

function solution(players, callings) {
    callings.forEach((val, i) => {
        const index = players.indexOf(val);
        const temp = players[index-1];
        players[index-1] = players[index];
        players[index] = temp;
    })
    return players;
}

ํ’€์ด โญ๏ธ

new Map()์„ ์ด์šฉํ•˜์—ฌ ๋ฏธ๋ฆฌ ์ธ๋ฑ์‹ฑ์„ ํ•ด๋‘๊ณ , ๊ฐ’์„ ์ฐพ์•„์˜ฌ ๋•Œ ๋ฐฐ์—ด์˜ indexOf๊ฐ€ ์•„๋‹Œ hash์˜ get์„ ์ด์šฉํ•ด ์ฐพ์•„์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ์ด๋ฆ„์„ ๋ถ€๋ฅธ ์ธ๋ฑ์Šค(ํ˜„์žฌ)์™€ ๊ทธ ์•ž์— ์žˆ๋Š” ์ธ๋ฑ์Šค์˜ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜์‹œ๊ณ , ๊ผญ hash๋„ ๋‹ค์‹œ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค!

 

function solution(players, callings) {
    const hash = new Map();
    
    players.forEach((val, i) => {
        hash.set(val, i);
    })

    callings.forEach((val, i) => {
        const index = hash.get(val);
        const front = players[index-1];
        
        const temp = players[index-1];
        players[index-1] = players[index];
        players[index] = temp;
        
        hash.set(val, hash.get(val)-1);
        hash.set(front, hash.get(val)+1);
    })
    return players;
}