DevYoon

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

PS/Programmers

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

gimewn 2022. 10. 28. 23:51

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/92334?language=javascript 

 

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

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

programmers.co.kr

 

๐Ÿ’ญ A ์œ ์ €๊ฐ€ B ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ–ˆ์„ ๊ฒฝ์šฐ 1๋ฒˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ค‘๋ณต์ฒดํฌํ•ด์ค˜์•ผ ํ•จ โžก๏ธ array๋ฅผ set์œผ๋กœ ๋งŒ๋“ค์–ด ์ค‘๋ณต ์ฒ˜๋ฆฌ

๐Ÿ’ญ C ์œ ์ €๊ฐ€ k๋ฒˆ ์ด์ƒ ์‹ ๊ณ  ๋‹นํ–ˆ๋‹ค๋ฉด, C ์œ ์ €๋ฅผ ์‹ ๊ณ ํ–ˆ๋˜ ์œ ์ €๋“ค์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ด์ค˜์•ผ ํ•จ

 

๐Ÿ’ญ ์œ„ ๋ถ€๋ถ„์„ ์ฒ˜์Œ์—” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ์ง์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ ค ํ–ˆ๋‹ค.

1. user ๊ฐ์ฒด์— user๋ณ„๋กœ ์‹ ๊ณ ํ•œ ํƒ€ ์œ ์ € ๋ฐฐ์—ด์„ value๋กœ ๋‹ด์•„์ค€๋‹ค. ex) { muzi : [frodo, apeach] }

2. report_cnt ๊ฐ์ฒด์— user๋ณ„๋กœ ์ž์‹ ์ด ์‹ ๊ณ  ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ value๋กœ ๋‹ด์•„์ค€๋‹ค. ex) { frodo : 2 }

3. report_cnt ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํšŸ์ˆ˜๊ฐ€ k๋ฒˆ ์ด์ƒ์ธ ๊ฒƒ์„ ์ฐพ์•„ freeze ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค.

4. freez ๋ฐฐ์—ด๊ณผ user ๊ฐ์ฒด๋ฅผ ์ด์ค‘์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ, freez ๋ฐฐ์—ด์— ๋“ค์–ด ์žˆ๋Š” ์œ ์ €๊ฐ€ user ๊ฐ์ฒด์˜ A ์œ ์ €์˜ value์— ๋“ค์–ด ์žˆ๋‹ค๋ฉด, A ์œ ์ €์˜ ๋ฉ”์ผ ์ˆ˜์‹  ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

 

๐Ÿ’ญ ๊ทธ๋Ÿฐ๋ฐ ๋กœ์ง์ด ๋„ˆ๋ฌด ๋ณต์žกํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ–ˆ๋‹ค.

1. reported ๊ฐ์ฒด์— user๋ณ„๋กœ ์ž์‹ ์„ ์‹ ๊ณ ํ•œ ํƒ€ ์œ ์ €๋ฅผ ๋ฐฐ์—ด๋กœ ๋‹ด๋Š”๋‹ค. ex) { frodo : [muzi, apeach] }

2. reported ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ value์˜ ๊ธธ์ด๊ฐ€ k ์ด์ƒ์ด๋ฉด, value์— ์†ํ•œ ์œ ์ €๋“ค์˜ ๋ฉ”์ผ ์ˆ˜์‹  ํšŸ์ˆ˜๋ฅผ +1 ํ•ด์ค€๋‹ค.

 

function solution(id_list, report, k) {
    var answer = [];
    let reported = {};
    let get_mail = {};
    
    /* ๋ฉ”์ผ ๋ฐ›์„ ํšŸ์ˆ˜๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ */
    id_list.map((user) => get_mail[user] = 0);
    
    /* ์ค‘๋ณต ์‹ ๊ณ  ์ œ๊ฑฐ */
    let set_report = [...new Set(report)];
    
    set_report.map((item) => {
    let [user, user_report] = [...item.split(" ")]
    /* user๋ณ„๋กœ ์‹ ๊ณ  ๋‹นํ•œ ํšŸ์ˆ˜ ์นด์šดํŠธ */
    reported[user_report] ? reported[user_report].push(user) :
    reported[user_report] = [user];
    })
    
    /* ์‹ ๊ณ  ๋‹นํ•œ ํšŸ์ˆ˜๊ฐ€ k ์ด์ƒ์ด๋ฉด ์‹ ๊ณ ํ•œ ์œ ์ €๋“ค์˜ ๋ฉ”์ผ ๊ฐฏ์ˆ˜ +1 */
    for(let user in reported){
        if(reported[user].length >= k){
            reported[user].map((who) => get_mail[who]++);
        }
    }
    
    /* ์œ ์ €๋ณ„๋กœ ๋ฐ›๋Š” ๋ฉ”์ผ ๊ฐฏ์ˆ˜ ์˜ค๋ธŒ์ ํŠธ์˜ value๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ */
    answer = Object.values(get_mail);
    
    return answer;
}