Jieunny์˜ ๋ธ”๋กœ๊ทธ

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

Study/Coding Test

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

Jieunny 2023. 2. 2. 16:06

๐Ÿ“Œ  ๋ฌธ์ œ

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

  • ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์‹ ๊ณ  ํšŸ์ˆ˜์— ์ œํ•œ์€ ์—†์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์œ ์ €๋ฅผ ๊ณ„์†ํ•ด์„œ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ์œ ์ €์— ๋Œ€ํ•œ ์‹ ๊ณ  ํšŸ์ˆ˜๋Š” 1ํšŒ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋œ ์œ ์ €๋Š” ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋˜๋ฉฐ, ํ•ด๋‹น ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ์œ ์ €์—๊ฒŒ ์ •์ง€ ์‚ฌ์‹ค์„ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.
    • ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ทจํ•ฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ํ•œ๊บผ๋ฒˆ์— ๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€๋ฅผ ์‹œํ‚ค๋ฉด์„œ ์ •์ง€ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ „์ฒด ์œ ์ € ๋ชฉ๋ก์ด ["muzi", "frodo", "apeach", "neo"]์ด๊ณ , k = 2(์ฆ‰, 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•˜๋ฉด ์ด์šฉ ์ •์ง€)์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์œ ์ € ID์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID์„ค๋ช…

"muzi" "frodo" "muzi"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"apeach" "frodo" "apeach"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"frodo" "neo" "frodo"๊ฐ€ "neo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"muzi" "neo" "muzi"๊ฐ€ "neo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"apeach" "muzi" "apeach"๊ฐ€ "muzi"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ ์ € ID์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜

"muzi" 1
"frodo" 2
"apeach" 0
"neo" 2

์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ "frodo"์™€ "neo"์˜ ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ํ•œ ์•„์ด๋””์™€ ์ •์ง€๋œ ์•„์ด๋””๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ ์ € ID์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID์ •์ง€๋œ ID

"muzi" ["frodo", "neo"] ["frodo", "neo"]
"frodo" ["neo"] ["neo"]
"apeach" ["muzi", "frodo"] ["frodo"]
"neo" ์—†์Œ ์—†์Œ

๋”ฐ๋ผ์„œ "muzi"๋Š” ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 2ํšŒ, "frodo"์™€ "apeach"๋Š” ๊ฐ๊ฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 1ํšŒ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

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


์ œํ•œ์‚ฌํ•ญ

  • 2 ≤ id_list์˜ ๊ธธ์ด ≤ 1,000
    • 1 ≤ id_list์˜ ์›์†Œ ๊ธธ์ด ≤ 10
    • id_list์˜ ์›์†Œ๋Š” ์ด์šฉ์ž์˜ id๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ด๋ฉฐ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • id_list์—๋Š” ๊ฐ™์€ ์•„์ด๋””๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋“ค์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • 1 ≤ report์˜ ๊ธธ์ด ≤ 200,000
    • 3 ≤ report์˜ ์›์†Œ ๊ธธ์ด ≤ 21
    • report์˜ ์›์†Œ๋Š” "์ด์šฉ์žid ์‹ ๊ณ ํ•œid"ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด "muzi frodo"์˜ ๊ฒฝ์šฐ "muzi"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
    • id๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ด์šฉ์žid์™€ ์‹ ๊ณ ํ•œid๋Š” ๊ณต๋ฐฑ(์ŠคํŽ˜์ด์Šค)ํ•˜๋‚˜๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ž๊ธฐ ์ž์‹ ์„ ์‹ ๊ณ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • 1 ≤ k ≤ 200, k๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • return ํ•˜๋Š” ๋ฐฐ์—ด์€ id_list์— ๋‹ด๊ธด id ์ˆœ์„œ๋Œ€๋กœ ๊ฐ ์œ ์ €๊ฐ€ ๋ฐ›์€ ๊ฒฐ๊ณผ ๋ฉ”์ผ ์ˆ˜๋ฅผ ๋‹ด์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ก  ์•„์ด๋””์–ด

id_list ๋Œ๋ฉด์„œ ๊ฒ์ฒด ๋งŒ๋“ค์–ด์„œ key๋Š” ์ด๋ฆ„, value๋Š” key๊ฐ€ ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ ๋ฐฐ์—ด์„ ํ• ๋‹นํ•œ๋‹ค -> whoReport

value ๋ฅผ ํ• ๋‹นํ•  ๋•Œ ์ด๋ฏธ ์‹ ๊ณ ํ•œ ์ด๋ ฅ์ด ์žˆ์œผ๋ฉด ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.

๊ฐ ์œ ์ €๊ฐ€ ๋ช‡ ๋ฒˆ ์‹ ๊ณ  ๋‹นํ–ˆ๋Š”์ง€ ์ €์žฅ ํ•  ๊ฐ์ฒด๋„ ๋งŒ๋“ ๋‹ค. -> reportCount

๋ฉ”์ผ ๋ช‡ ๋ฒˆ ๋ฐ›์•˜๋Š”์ง€ ์ €์žฅ ํ•  ๊ฐ์ฒด๋„ ๋งŒ๋“ ๋‹ค -> getMail

reportCount ๋Œ๋ฉด์„œ k ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด, whoReport ์—์„œ ๊ทธ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ์œ ์ €๋ฅผ ์ฐพ๊ณ , ๊ทธ ์œ ์ €์˜ getMail ๊ฐ’์„ ++ ํ•ด์ค€๋‹ค.

 

โœ๏ธ  ํ’€์ด

function solution(id_list, report, k) {
  var answer = [];

  const whoReport = {};	// ๋ˆ„๊ฐ€ ๋ˆ„๊ตฌ ์‹ ๊ณ ํ–ˆ๋Š”์ง€ ๋‹ด์„ ๊ฐ์ฒด
  const reportCount = {};	// ๋ˆ„๊ฐ€ ๋ช‡ ๋ฒˆ ์‹ ๊ณ ๋‹นํ–ˆ๋Š”์ง€ ๋‹ด์„ ๊ฐ์ฒด
  const getMail = {};	// ๋ˆ„๊ฐ€ ๋ช‡ ๋ฒˆ ๋ฉ”์ผ ๋ฐ›์•˜๋Š”์ง€ ๋‹ด์„ ๊ฐ์ฒด

  for(i=0; i<id_list.length; i++){	
  // id_list ๋Œ๋ฉด์„œ ๊ฐ ๊ฐ์ฒด์— ์œ ์ € ์ด๋ฆ„ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.
    whoReport[id_list[i]] = [];
    reportCount[id_list[i]] = 0;
    getMail[id_list[i]] = 0;
  }

  for(let i=0; i<report.length; i++){	
  // ์‹ ๊ณ  ํ•œ ์‚ฌ๋žŒ๊ณผ ๋‹นํ•œ ์‚ฌ๋žŒ์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ชผ๊ฐœ์„œ whoReport์— ๋ˆ„๊ฐ€ ๋ˆ„๊ตด ์‹ ๊ณ ํ–ˆ๋Š”์ง€ ํ• ๋‹นํ•ด์ค€๋‹ค
    let temp = report[i].split(' ');
    if(whoReport[temp[0]].includes(temp[1]) === false){
      whoReport[temp[0]].push(temp[1]);
    }  
  }

  for(let key in whoReport){
  // ์‹ ๊ณ  ๋‹นํ•œ ์‚ฌ๋žŒ ์ด๋ฆ„ ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ๋ˆ„๊ฐ€ ๋ช‡ ๋ฒˆ ์‹ ๊ณ  ๋‹นํ–ˆ๋Š”์ง€ ++ ํ•ด์ค€๋‹ค.
    for(let i=0; i<whoReport[key].length; i++){
      reportCount[whoReport[key][i]] ++;
    }
  }

  for(let key in reportCount){
  // k๋งŒํผ ๋˜๋Š” ๋” ์‹ ๊ณ  ๋‹นํ•œ ์œ ์ €๋ฅผ ์ฐพ์•„์„œ ๊ทธ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ์œ ์ € ๋ฉ”์ผ ํšŸ์ˆ˜๋ฅผ ++ ํ•ด์ค€๋‹ค.
    if(reportCount[key] >= k){
      for(let name in whoReport){
        if(whoReport[name].includes(key)){
          getMail[name]++;
        }
      }
    }
  }

  for(let name in getMail){
  // ๊ฐ ์œ ์ €๊ฐ€ ๋ฉ”์ผ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ pushํ•ด์ค€๋‹ค.
    answer.push(getMail[name]);
  }

  return answer;
}