๐Ÿ’ก Level 1 | ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ (JavaScript)

rimmzยท2022๋…„ 7์›” 11์ผ
0

programmers

๋ชฉ๋ก ๋ณด๊ธฐ
15/15
post-thumbnail

๐Ÿ“Œ ๋ฌธ์ œ

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

  • ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • ์‹ ๊ณ  ํšŸ์ˆ˜์— ์ œํ•œ์€ ์—†์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์œ ์ €๋ฅผ ๊ณ„์†ํ•ด์„œ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ์œ ์ €์— ๋Œ€ํ•œ ์‹ ๊ณ  ํšŸ์ˆ˜๋Š” 1ํšŒ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

  • k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋œ ์œ ์ €๋Š” ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋˜๋ฉฐ, ํ•ด๋‹น ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ์œ ์ €์—๊ฒŒ ์ •์ง€ ์‚ฌ์‹ค์„ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.

    • ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ทจํ•ฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ํ•œ๊บผ๋ฒˆ์— ๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€๋ฅผ ์‹œํ‚ค๋ฉด์„œ ์ •์ง€ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.

์ด์šฉ์ž์˜ ID๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด id_list, ๊ฐ ์ด์šฉ์ž๊ฐ€ ์‹ ๊ณ ํ•œ ์ด์šฉ์ž์˜ ID ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด report, ์ •์ง€ ๊ธฐ์ค€์ด ๋˜๋Š” ์‹ ๊ณ  ํšŸ์ˆ˜ k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ ์œ ์ €๋ณ„๋กœ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

https://programmers.co.kr/learn/courses/30/lessons/92334

๐ŸŒฟ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

function solution(id_list, report, k) {
  let reports = [...new Set(report)].map((a) => {
    return a.split(" ");
  });

  let counts = new Map();
  for (const bad of reports) {
    counts.set(bad[1], counts.get(bad[1]) + 1 || 1);
  }

  let good = new Map();
  for (const report of reports) {
    if (counts.get(report[1]) >= k) {
      good.set(report[0], good.get(report[0]) + 1 || 1);
    }
  }

  let answer = id_list.map((a) => good.get(a) || 0);
  
  return answer;
}

๐Ÿ’ฌ ์ •๋ฆฌ

Map()

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ key-value๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜
  • key-value์˜ ์Œ์„ ์ €์žฅํ•˜๋ฉฐ ๊ฐ ์Œ์˜ ์‚ฝ์ž… ์ˆœ์„œ๋„ ๊ธฐ์–ต
  • key ๋ฅผ ์‚ฌ์šฉํ•ด์„œ value ๋ฅผ get, set ํ•  ์ˆ˜ ์žˆ์Œ

โœ”๏ธ Map ์ฃผ์š” ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ
new Map() : Map ์ƒ์„ฑ
map.set(key, value) : key๋ฅผ ์ด์šฉํ•ด value๋ฅผ ์ €์žฅํ•œ๋‹ค.
map.get(key) : 'key'์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (key๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜)

Set()

  • ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๋ชจ์•„ ๋†“์€ ํŠน๋ณ„ํ•œ ์ปฌ๋ ‰์…˜
  • Set() ์€ value ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜

โœ”๏ธ Set ์ฃผ์š” ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ
new Set(iterable) : Set ์ƒ์„ฑ
set.add(value) : ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์…‹ ์ž์‹ ์„ ๋ฐ˜ํ™˜
set.delete(value) : ๊ฐ’์„ ์ œ๊ฑฐ

๐Ÿ”— ์ฐธ๊ณ 

https://velog.io/@proshy/JSSet-Map-Object-%EC%A0%95%EB%A6%AC
https://lakelouise.tistory.com/38

profile
#์˜์š•๋„˜์น˜๋Š”#๐Ÿ’ป#โœจ#FE#๐Ÿ’ช๐Ÿป

0๊ฐœ์˜ ๋Œ“๊ธ€