๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป
article thumbnail
๋ฐ˜์‘ํ˜•

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ '๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ' ๋ฌธ์ œ ํ’€์ด (LV.1)

โ“ ๋ฌธ์ œ 

๋ฌธ์ œ ์„ค๋ช…

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

์ œํ•œ ์‚ฌํ•ญ

  • 5 ≤ players์˜ ๊ธธ์ด ≤ 50,000
    • players[i]๋Š” i๋ฒˆ์งธ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • players์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • players์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 3 ≤ players[i]์˜ ๊ธธ์ด ≤ 10
  • 2 ≤ callings์˜ ๊ธธ์ด ≤ 1,000,000
    • callings๋Š” players์˜ ์›์†Œ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ฒฝ์ฃผ ์ง„ํ–‰์ค‘ 1๋“ฑ์ธ ์„ ์ˆ˜์˜ ์ด๋ฆ„์€ ๋ถˆ๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

4๋“ฑ์ธ "kai" ์„ ์ˆ˜๊ฐ€ 2๋ฒˆ ์ถ”์›”ํ•˜์—ฌ 2๋“ฑ์ด ๋˜๊ณ  ์•ž์„œ 3๋“ฑ, 2๋“ฑ์ธ "poe", "soe" ์„ ์ˆ˜๋Š” 4๋“ฑ, 3๋“ฑ์ด ๋ฉ๋‹ˆ๋‹ค. 5๋“ฑ์ธ "mine" ์„ ์ˆ˜๊ฐ€ 2๋ฒˆ ์ถ”์›”ํ•˜์—ฌ 4๋“ฑ, 3๋“ฑ์ธ "poe", "soe" ์„ ์ˆ˜๊ฐ€ 5๋“ฑ, 4๋“ฑ์ด ๋˜๊ณ  ๊ฒฝ์ฃผ๊ฐ€ ๋๋‚ฉ๋‹ˆ๋‹ค. 1๋“ฑ๋ถ€ํ„ฐ ๋ฐฐ์—ด์— ๋‹ด์œผ๋ฉด ["mumu", "kai", "mine", "soe", "poe"]์ด ๋ฉ๋‹ˆ๋‹ค.

 

โŒ ์ฒ˜์Œ ์‹คํŒจํ•œ ํ’€์ด

์ฒ˜์Œ ํ’€์ด์—๋Š” caliing ๋ฐฐ์—ด ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— players์— ๋Œ€ํ•œ indexOf๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ํ…Œ์ด์ŠคํŠธ ์ผ€์ด์Šค 3~4๊ฐœ ์ •๋„์—์„œ ์‹คํŒจ๊ฐ€ ๋–ด๋‹ค.

์ด์œ ๋Š” ์ œํ•œ ์‚ฌํ•ญ์„ ๋ณด๋ฉด players์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 50,000์ด๊ณ  callings์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 1,000,000์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์•ˆ์— indexOf๋ฅผ ์‚ฌ์šฉํ•ด์„œ O(n^2)์ด ๋˜๋Š”๋ฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ์— 50,000,000,000๋ฒˆ ์—ฐ์‚ฐ์„ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋‚˜์™€์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ ๊ฐ™๋‹ค.

function solution(players, callings) {
    const hash = new Map();
    
    callings.forEach(name => {
       const currIdx = players.indexOf(name);
        [players[currIdx], players[currIdx - 1]] = [players[currIdx - 1], players[currIdx]];
    })
    
    return players;
}
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์„ฑ๊ณตํ•œ ํ’€์ด

function solution(players, callings) {
    const hash = new Map();
    
    players.forEach((name, index) => {
        hash.set(name, index);
    })
    
    callings.forEach(name => {
        const currIdx = hash.get(name);
        const front = players[currIdx - 1];

        [players[currIdx], players[currIdx -1]] = [players[currIdx -1], players[currIdx]];
        
        hash.set(name, hash.get(name) - 1);
        hash.set(front, hash.get(name) + 1);
    })
    
    return players;
}

hash ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ดํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๊ฟจ๋‹ค.

object์˜ key๋กœ ์ ‘๊ทผํ•  ๋•Œ bigO๋Š” O(1)์ด๋‹ค.

 

๋จผ์ €, players์˜ name์„ key, ํ•ด๋‹น index๋ฅผ value๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์—ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ callings์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋Š”๋ฐ,

์ดˆ๊ธฐํ™”ํ•ด์ฃผ์—ˆ๋˜ hash์— callings์— ๋Œ€ํ•œ ์š”์†Œ(์ด๋ฆ„)๋กœ key์— ์ ‘๊ทผํ•ด์„œ value๋ฅผ currIdx์— ๋‹ด์•„๋‘์—ˆ๋‹ค.

๊ทธ๋Ÿผ ํ•ด๋‹น ์ด๋ฆ„์˜ index๋ฅผ ๋ฐ›์•„์™”๊ณ  ์ด๋ฆ„์ด ๋ถˆ๋ฆฐ ์•ž์‚ฌ๋žŒ๊ณผ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.

์•ž์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ fornt๋ผ๋Š” ๋ณ€์ˆ˜์— currIdx - 1 ๊ฐ’์„ ๋‹ด์•„๋‘์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์„œ๋กœ์˜ index ๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ๊ณ ,

 

hash์— ๋ฐ”๋€ index๋กœ ์„œ๋กœ ๋ฐ”๊ฟ” ์ฃผ๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋๊นŒ์ง€ ๋Œ๊ฒŒ ๋˜๋ฉด ๋์ด ๋‚œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— indexOf๋ฅผ ๋Œ๋ ธ์„ ๋•Œ๋Š” O(N^2)์ด์ง€๋งŒ hash๋ฅผ ์ด์šฉํ•ด์„œ O(N)์œผ๋กœ ํ•ด๊ฒฐํ•ด ์ฃผ์—ˆ๋‹ค.

โœ” ์ฑ„์ 

์ฑ„์  ๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•
profile

๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป

@์šฉ๋‡ฝ

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!