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

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

๐Ÿ”ฅ Algorithm/Programmers

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

Lennon 2022. 1. 22. 08:08
728x90
๋ฐ˜์‘ํ˜•

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๋ฌธ์ œ ์„ค๋ช… ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜

programmers.co.kr

function solution(id_list, report, k) {
    report = [...new Set(report)].map(v => v.split(' ')); 
    const reported = report.map(v => v[1]);
    const reportCount = new Array(id_list.length).fill(0);
    const kReported = [];
    
    reported.forEach(v => {
        reportCount[id_list.indexOf(v)]++;
    })
    
    reportCount.forEach((v,idx) => {
        if(v >= k){
            kReported.push(id_list[idx]);  
        }
    })
    
    const answer = new Array(id_list.length).fill(0);
    
    report.forEach((v,idx) => {
        if(kReported.includes(v[1])){
            answer[id_list.indexOf(v[0])]++;
        }
    })

    return answer
    
}

๋จผ์ € ์‹ ๊ณ ๋Š” ํ•˜๋‚˜๋งŒ ์ ์šฉ๋˜๋ฏ€๋กœ Set์„ ์ด์šฉํ•ด report์— ๋Œ€ํ•ด ์ค‘๋ณต๊ฐ’์„ ์—†์• ์ค€๋‹ค.

reported => ์‹ ๊ณ ๋œ ์‚ฌ๋žŒ๋“ค ๋ฐฐ์—ด

reportCount => ์‹ ๊ณ  ํšŸ์ˆ˜ ๊ฒ€์ฆ์„ ์œ„ํ•œ ๋ฐฐ์—ด

kReported => k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋ฐ›์€ ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ ๋ฐฐ์—ด

 

7-9 line

์‚ฌ๋žŒ ๋ณ„ ์‹ ๊ณ  ํšŸ์ˆ˜๋ฅผ ์„ธ์–ด reportCount์— ์ €์žฅ (indexOf๋ฅผ ํ™œ์šฉ)

 

11-15 line

์‹ ๊ณ ํšŸ์ˆ˜๊ฐ€ k๋ณด๋‹ค ๋†’์€ ์‚ฌ๋žŒ์„ kReported ๋ฐฐ์—ด์— push

 

19-23 line

report๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋ฅผ ๋‹นํ•œ ์‚ฌ๋žŒ์„ ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์˜ ํšŸ์ˆ˜๋ฅผ 1๊ฐœ์”ฉ ์ฆ๊ฐ€

 

์นด์นด์˜คํ†ก 2022 ๊ณต์ฑ„ ์œ ์ผํ•œ 1๋‹จ๊ณ„ ๋ฌธ์ œ์ด๋‹ค. ๋‹จ์ˆœ ๊ตฌํ˜„๋ฌธ์ œ๋ผ ๋Œ€๋ถ€๋ถ„์ด ๋งž์•˜์„๊ฑฐ๋ผ ์ƒ๊ฐํ•œ๋‹ค.

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