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

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ

728x90
function solution(players, callings) {
    //player ์˜ ๋ฐฐ์—ด -> callings ์˜ ์ˆœ์„œ ์— ํ•ด๋‹นํ•œ๋Š” ์„ ์ˆ˜์™€ ๊ทธ ์•ž์˜ ์„ ์ˆ˜์˜ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ”๊ฟˆ
    //callings ๋ฅผ foreach ๋กœ ๋ˆ๋‹ค
    //callings ์— ์ด๋ฆ„์„ players ์—์„œ indexOf ๋กœ ์ฐพ๊ณ , 
    //index-1 ๊ณผ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พผ๋‹ค. => ์ด์ „ ์„ ์ˆ˜๋ฅผ ์ฐพ๊ณ , ๋จผ์ € ์ด๋ฆ„์„ ๋„ฃ๋Š”๋‹ค.
    
    callings.forEach(call => {
        const callIndex = players.findIndex(v => v === call)
        const fall = players[callIndex - 1];
        players[callIndex] = fall;
        players[callIndex - 1] = call;
        console.log(players)
    });
    return players
}

 

์ฒจ์— ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆœ์„œ๋ฅผ ๋จผ์ € ์“ฐ๊ณ  ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, 

์˜ˆ์ƒ๋Œ€๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

 

for ๋ฌธ ์•ˆ์—์„œ findIndex ๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— n^2 ๊ฐ€ ๋œ ๋“ฏ,,, ํ•˜๋‹ค!

 

๊ทธ๋ ‡๋‹ค๋ฉด index ๋ฅผ ๊ณ„์† ์ฐพ๋Š” ๊ฒƒ ๋Œ€์‹  ๋‹ค๋ฅด๊ฒŒ ๊ธฐ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผํ–ˆ๋‹ค.

map ์„ ์ด์šฉํ•ด ํ’€์–ด๋ดค๋‹ค.

function solution(players, callings) {
    const order = new Map();
    players.forEach((name, index) => {
        order.set(name, index);
    })
    callings.forEach(call => {
        const callIndex = order.get(call)
        const fall = players[callIndex - 1];
        players[callIndex] = fall;
        players[callIndex - 1] = call;
        order.set(call, order.get(call) - 1);
        order.set(fall, order.get(call) + 1);
    });
    return players
}
Map(5) { 'mumu' => 0, 'soe' => 3, 'poe' => 4, 'kai' => 1, 'mine' => 2 } 
[ 'mumu', 'kai', 'mine', 'soe', 'poe' ]

 

๊ทธ๋ƒฅ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋Š”๋ฐ, 

map ์„ ์‚ฌ์šฉํ•œ ๊ทผ๊ฑฐ๊ฐ€ ์‚ฌ์‹ค ์—†์—ˆ๋‹ค... 

๊ทธ๋ž˜์„œ ์ฐพ์•„๋ด„. 

์•„๋ž˜ ๋งํฌ์— ๊ทธ ์ฐจ์ด๊ฐ€ ์ž˜ ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Map#%EA%B0%9D%EC%B2%B4_vs_%EB%A7%B5

 

Map - JavaScript | MDN

Map ๊ฐ์ฒด๋Š” ํ‚ค-๊ฐ’ ์Œ๊ณผ ํ‚ค์˜ ์›๋ž˜ ์‚ฝ์ž… ์ˆœ์„œ๋ฅผ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฐ’(๊ฐ์ฒด ๋ฐ ์›์‹œ ๊ฐ’ ๋ชจ๋‘)์€ ํ‚ค ๋˜๋Š” ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

developer.mozilla.org

 

 

Map ๊ฐ์ฒด๋Š” ํ‚ค-๊ฐ’ ์Œ์ธ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ํ•œ Map ์—์„œ์˜ ํ‚ค๋Š” ์˜ค์ง ๋‹จ ํ•˜๋‚˜๋งŒ ์กด์žฌ ํ•ฉ๋‹ˆ๋‹ค.
์ด๋Š” Map ์ง‘ํ•ฉ์˜ ์œ ์ผ์„ฑ์ž…๋‹ˆ๋‹ค. 

 

์ด์ฏค ๋˜๋‹ˆ ์–ผ๋งˆ๋‚˜ ์ฐจ์ด๋‚˜๋‚˜ ๊ถ๊ธˆํ•ด์ง. 

 

function solution(players, callings) {
    const order = {};
    players.forEach((name, index) => {
        order[name] = index;
    })
    callings.forEach(call => {
        const callIndex = order[call];
        const fall = players[callIndex - 1];
        players[callIndex] = fall;
        players[callIndex - 1] = call;
        order[call] = callIndex - 1;
        order[fall] = callIndex;
    });
    return players
}

 

 

Map ์„ ์ด์šฉํ•œ ๊ฒฐ๊ณผ vs Object ๋ฅผ ์ด์šฉํ•œ ๊ฒฐ๊ณผ

 

์˜ค map ์„ ์‚ฌ์šฉํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋Œ€์ฒด๋กœ ๋” ์ ์–ด๋ณด์ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ์ˆ˜๊ฐ€ ์ œ์ผ ๋งŽ์€ 11, 12, 13 ์ผ€์ด์Šค์—์„  map ์ด ๋ฌด์กฐ๊ฑด ๋น ๋ฅด๋‹ค๊ณ  ํ•  ์ˆœ ์—†์ง€๋งŒ 

11๋ฒˆ ์ฒ˜๋Ÿผ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ™• ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. 

 

์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ํด ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋  ๋•Œ map ์„ ์ž˜ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„ ์— ์œ ์˜๋ฏธํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

 

728x90
๋ฐ˜์‘ํ˜•