Home ๐Ÿ„๐Ÿปโ€โ™€๏ธ [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ
Post
Cancel

๐Ÿ„๐Ÿปโ€โ™€๏ธ [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๐Ÿ“„ ๋ฌธ์ œ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2022 KAKAO BLIND RECRUITMENT ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€

ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ „์ฒด ์‹ ๊ณ  ๋ชฉ๋ก(report)์—์„œ 1๏ธโƒฃ์œ ์ €๋ณ„๋กœ ๋ณธ์ธ์ด ์‹ ๊ณ ํ•œ ๊ธฐ๋ก์„ ์ €์žฅํ•˜๊ณ  2๏ธโƒฃ๊ฐ ์œ ์ €๊ฐ€ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๊ธฐ๋กํ•ด๋‘”๋‹ค.

๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€ ํšŸ์ˆ˜(k)์— ๋ถ€ํ•ฉํ•˜๋Š” ์ด์šฉ์ž๋“ค์„ ํ™•์ธํ•˜๊ณ  ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ํ•œ ๊ธฐ๋ก์ด ๋‹ด๊ธด ๊ฐ์ฒด์—์„œ ์ด์šฉ ์ •์ง€ ๋Œ€์ƒ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฉ”์ผ ์ „์†ก ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ์ด๋ฅผ ์ƒˆ ๋ฐฐ์—ด์— ์ €์žฅ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿน ์‹œ๋„

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
32
33
34
35
36
function solution(id_list, report, k) {
  //1๏ธโƒฃ์œ ์ €๋ณ„๋กœ ๋ณธ์ธ์ด ์‹ ๊ณ ํ•œ ๊ธฐ๋ก์„ ์ €์žฅํ•  ๊ฐ์ฒด
  let users = {};
  //2๏ธโƒฃ๊ฐ ์œ ์ €๊ฐ€ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๊ฐ์ฒด
  let reportCount = {};

  //์‹ ๊ณ  ๋ชฉ๋ก์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ Set ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.
  id_list.forEach((id) => {
    users[id] = new Set();
  });

  //์ „์ฒด ์‹ ๊ณ  ๋ชฉ๋ก์„ ๋ฐ˜๋ณตํ•ด์„œ ์œ ์ €๋ณ„ ์‹ ๊ณ  ๊ธฐ๋ก์„ ์—…๋ฐ์ดํŠธ
  report.forEach((r) => {
    //id : ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ, reported : ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ
    const [id, reported] = r.split(" ");

    //๋™์ผํ•œ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ๊ธฐ๋ก์ด ์žˆ๋Š”์ง€ ์ฒดํฌ
    if (!users[id].has(reported)) {
      //์‹ ๊ณ  ๊ธฐ๋ก์ด ์—†์œผ๋ฉด ์‹ ๊ณ  ๊ธฐ๋ก์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ ๊ณ ๋‹นํ•œ ์œ ์ €์˜ ์‹ ๊ณ  ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
      users[id].add(reported);
      reportCount[reported] = (reportCount[reported] || 0) + 1;
    }
  });

  return Object.keys(users).map((key) => {
    const current = users[key];
    let count = 0;
    //์œ ์ €๋ณ„ ์‹ ๊ณ  ๊ธฐ๋ก์—์„œ ๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€ ๋Œ€์ƒ์— ๋ถ€ํ•ฉํ•˜๋Š” ์œ ์ €๊ฐ€ ์žˆ์œผ๋ฉด count๋ฅผ ์ฆ๊ฐ€
    for (let r of current) {
      if (reportCount[r] >= k) {
        count++;
      }
    }
    return count;
  });
}

โ›ณ๏ธ ๋” ์ข‹์€ ํ•ด๊ฒฐ์ฑ…

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด์—์„œ ๊ฐ€์žฅ ๋งŽ์€ ์ข‹์•„์š”๋ฅผ ๋ฐ›์€ ์ฝ”๋“œ์ด๋‹ค. ์ค‘๋ณต๋œ ์‹ ๊ณ  ๋ชฉ๋ก์€ ํ•œ ๊ฑด์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋‹ˆ๊นŒ report์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐ ํ›„ ๋‹ค๋ฅธ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ์ค‘๋ณต ์ œ๊ฑฐ๋Š” Set์„ ์ผ๊ณ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function solution(id_list, report, k) {
  //์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด Set์„ ์‚ฌ์šฉํ•˜์˜€๊ณ  ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ ํ›„ split๋กœ ์‹ ๊ณ ํ•œ ์œ ์ €์™€ ์‹ ๊ณ ๋‹นํ•œ ์œ ์ €๋ฅผ ๋ถ„๋ฆฌํ•˜์˜€๋‹ค.
  //[[ 'muzi', 'frodo' ]] ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์‹ ๊ณ  ๋‚ด์šฉ์ด ์ €์žฅ๋œ๋‹ค.
  let reports = [...new Set(report)].map((a) => {
    return a.split(" ");
  });
  let counts = new Map();
  //์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š”๋ฐ Map์„ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ณ  || ์—ฐ์‚ฐ์ž๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.
  for (const bad of reports) {
    counts.set(bad[1], counts.get(bad[1]) + 1 || 1);
  }
  let good = new Map();
  //๋ฉ”์ผ ์ „์†ก ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š”๋ฐ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ์˜ ํšŸ์ˆ˜๊ฐ€ k์™€ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฉด ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์˜ ๋ฉ”์ผ ์ „์†ก ํšŸ์ˆ˜๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
  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;
}

๐Ÿ‘ฉโ€๐ŸŒพ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋˜์—ˆ๊ฑฐ๋‚˜ ์ค‘์š”ํ•œ ํฌ์ธํŠธ

๋ฐฐ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•

  1. โญ๏ธ Set โ‡’ ์ค‘๋ณต ์ œ๊ฑฐ๋งŒ์„ ์œ„ํ•ด์„  ์ œ์ผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

    1
    2
    
    const report = ["ryan con", "ryan con", "ryan con", "ryan con"];
    const reports = [...new Set(report)]; // ["ryan con"]
    
  2. filter

    1
    2
    3
    4
    
    const report = ["ryan con", "ryan con", "ryan con", "ryan con"];
    const reports = report.filter((item, index) => {
      return report.indexOf(item) === index;
    }); //["ryan con"]
    
  3. Map

    1
    2
    3
    4
    5
    6
    
    const report = ["ryan con", "ryan con", "ryan con", "ryan con"];
    const objUnique = {};
    report.forEach((el) => {
      objUnique[el] = true;
    });
    const arrUnique = Object.keys(objUnique); // ["ryan con"]
    
This post is licensed under CC BY 4.0 by the author.

์ฐธ์กฐ ํƒ€์ž…์˜ ๋ถˆ๋ณ€์„ฑ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“ธ 2022-08-02