Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ (js)

Lennon 2021. 10. 28. 19:31
728x90
๋ฐ˜์‘ํ˜•

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ

์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ ์นด์นด์˜คํ†ก ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ์—์„œ๋Š” ์นœ๊ตฌ๊ฐ€ ์•„๋‹Œ ์‚ฌ๋žŒ๋“ค๊ณผ ๋Œ€ํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ณธ๋ž˜ ๋‹‰๋„ค์ž„์ด ์•„๋‹Œ ๊ฐ€์ƒ์˜ ๋‹‰๋„ค์ž„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์‹ ์ž…์‚ฌ์›์ธ ๊น€ํฌ๋ฃจ๋Š” ์นด์นด์˜คํ†ก ์˜ค

programmers.co.kr

 

function solution(record) {
    let map = new Map();
    let arr = [];
    for(let i = 0; i < record.length; i++){
        const [state, id, name] = record[i].split(" ");
        
        if(state !== 'Change'){
            if(state==='Enter'){
                arr.push({id: id, state: "๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค."});
            }
            else {
                arr.push({id: id, state: "๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค."});
                continue;
            }
        }
        map.set(id, name);
    }
    
    return arr.map((v) => map.get(v.id)+v.state)
}

 

ํ•ด๋‹น ๋ฌธ์ œ์˜ id์™€ name์€ ๊ฒฐ๊ตญ ๋งˆ์ง€๋ง‰ change๋‚˜ enter๋กœ ๊ฒฐ์ •๋‚˜๋ฏ€๋กœ

 

map์„ ์ด์šฉํ•ด ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๊ณ„์† set ์‹œ์ผฐ๋‹ค.

 

๊ทธ ํ›„ ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค, ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค ๊ฐ์ฒด id๋ฅผ map์—์„œ ๊ฐ€์ ธ์™€ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋~~

 

์ด๋Ÿฐ ๋์— ๋ชจ๋“  ๊ฑธ ๊ฒฐ์ •ํ•˜๋Š” ๋ฌธ์ œ๋Š” set์ด๋‚˜ map์„ ์ด์šฉํ•˜๋ฉด ํšจ์œจ์ ์ด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๊นจ๋‹ฌ์•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments