|
2 | 2 | //완벽한 정답이 아닙니다.
|
3 | 3 | //정답 1 - codeisneverodd
|
4 | 4 | function solution(id_list, report, k) {
|
5 | | - //report를 set을 이용하여 중복제거, 각 id 당 신고당한 횟수 reportedCount에 저장, |
6 | | - //각 id를 신고한 사람 array를 reportedBy에 저장, |
7 | | - //k번 이상 신고당한 id를 신고한 id가 받을 메일 수를 mailCount에 저장 |
8 | | - //answer에 mailCount에 저장된 값을 id_list와 같은 id 순서로 저장. |
9 | | - const reportSet = new Set(report) |
10 | | - const reportedCount = {} //{"id": Number(count)} |
11 | | - const reportedBy = {} //{"id":[]} |
12 | | - const mailCount = {} //{"id":Number(count)} |
13 | | - id_list.forEach((element) => { |
14 | | - //Initialization |
15 | | - reportedCount[element] = 0 |
16 | | - mailCount[element] = 0 |
17 | | - reportedBy[element] = [] |
18 | | - }) |
19 | | - reportSet.forEach((element) => { |
20 | | - const $id = element.split(" ")[0] |
21 | | - const $reported = element.split(" ")[1] |
22 | | - reportedCount[$reported] += 1 |
23 | | - reportedBy[$reported].push($id) |
24 | | - }) |
25 | | - for (const reportedId in reportedCount) { |
26 | | - if (reportedCount[reportedId] >= k) { |
27 | | - reportedBy[reportedId].forEach((reporter) => { |
28 | | - mailCount[reporter] += 1 |
29 | | - }) |
| 5 | + //report를 set을 이용하여 중복제거, 각 id 당 신고당한 횟수 reportedCount에 저장, |
| 6 | + //각 id를 신고한 사람 array를 reportedBy에 저장, |
| 7 | + //k번 이상 신고당한 id를 신고한 id가 받을 메일 수를 mailCount에 저장 |
| 8 | + //answer에 mailCount에 저장된 값을 id_list와 같은 id 순서로 저장. |
| 9 | + const reportSet = new Set(report); |
| 10 | + const reportedCount = {}; //{"id": Number(count)} |
| 11 | + const reportedBy = {}; //{"id":[]} |
| 12 | + const mailCount = {}; //{"id":Number(count)} |
| 13 | + id_list.forEach((element) => { |
| 14 | + //Initialization |
| 15 | + reportedCount[element] = 0; |
| 16 | + mailCount[element] = 0; |
| 17 | + reportedBy[element] = []; |
| 18 | + }); |
| 19 | + reportSet.forEach((element) => { |
| 20 | + const $id = element.split(" ")[0]; |
| 21 | + const $reported = element.split(" ")[1]; |
| 22 | + reportedCount[$reported] += 1; |
| 23 | + reportedBy[$reported].push($id); |
| 24 | + }); |
| 25 | + for (const reportedId in reportedCount) { |
| 26 | + if (reportedCount[reportedId] >= k) { |
| 27 | + reportedBy[reportedId].forEach((reporter) => { |
| 28 | + mailCount[reporter] += 1; |
| 29 | + }); |
| 30 | + } |
30 | 31 | }
|
31 | | - } |
32 | | - var answer = [] |
33 | | - id_list.forEach((element) => { |
34 | | - answer.push(mailCount[element]) |
35 | | - }) |
| 32 | + var answer = []; |
| 33 | + id_list.forEach((element) => { |
| 34 | + answer.push(mailCount[element]); |
| 35 | + }); |
36 | 36 |
|
37 | | - return answer |
| 37 | + return answer; |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | //정답 2 - jaewon1676
|
41 | 41 | function solution(id_list, report, k) {
|
42 | | - let answer = new Array(id_list.length).fill(0) |
43 | | - let report_list = {} // 신고당한 ID |
| 42 | + let answer = new Array(id_list.length).fill(0); |
| 43 | + let report_list = {}; // 신고당한 ID |
44 | 44 |
|
45 | | - // key, value 형식의 report_list 객체를 만든다. |
46 | | - id_list.map((user) => { |
47 | | - report_list[user] = [] //key = userid , value = 빈 배열을 가지는 객체 |
48 | | - }) |
49 | | - // report_list { muzi: [], frodo: [], apeach: [], neo: [] } |
| 45 | + // key, value 형식의 report_list 객체를 만든다. |
| 46 | + id_list.map((user) => { |
| 47 | + report_list[user] = []; //key = userid , value = 빈 배열을 가지는 객체 |
| 48 | + }); |
| 49 | + // report_list { muzi: [], frodo: [], apeach: [], neo: [] } |
50 | 50 |
|
51 | | - // 유저가 신고한 ID를 report_list 객체에 넣어주기 위해 순회한다. |
52 | | - report.map((user) => { |
53 | | - const [user_id, report_id] = user.split(" ") |
54 | | - // report 값에서 띄어쓰기로 구분된 문자열을 자르고 user_id, report_id로 각각 넣어준다. |
55 | | - if (!report_list[report_id].includes(user_id)) { |
56 | | - report_list[report_id].push(user_id) |
57 | | - // 배열에 포함하는지 여부를 확인하여 포함하지 않을때 신고자의 이름을 추가하였다. |
| 51 | + // 유저가 신고한 ID를 report_list 객체에 넣어주기 위해 순회한다. |
| 52 | + report.map((user) => { |
| 53 | + const [user_id, report_id] = user.split(" "); |
| 54 | + // report 값에서 띄어쓰기로 구분된 문자열을 자르고 user_id, report_id로 각각 넣어준다. |
| 55 | + if (!report_list[report_id].includes(user_id)) { |
| 56 | + report_list[report_id].push(user_id); |
| 57 | + // 배열에 포함하는지 여부를 확인하여 포함하지 않을때 신고자의 이름을 추가하였다. |
| 58 | + } |
| 59 | + }); |
| 60 | + for (const key in report_list) { |
| 61 | + // report_list의 index 순회 |
| 62 | + if (report_list[key].length >= k) { |
| 63 | + report_list[key].map((user) => { |
| 64 | + answer[id_list.indexOf(user)] += 1; |
| 65 | + }); |
| 66 | + } |
58 | 67 | }
|
59 | | - }) |
60 | | - for (const key in report_list) { |
61 | | - // report_list의 index 순회 |
62 | | - if (report_list[key].length >= k) { |
63 | | - report_list[key].map((user) => { |
64 | | - answer[id_list.indexOf(user)] += 1 |
65 | | - }) |
66 | | - } |
67 | | - } |
68 | | - return answer |
| 68 | + return answer; |
69 | 69 | }
|
70 | 70 |
|
71 | 71 | //정답 3 - prove-ability
|
72 | 72 | function solution(id_list, report, k) {
|
73 | | - // 사용자 길이의 빈 배열을 만든다 0 |
74 | | - var answer = new Array(id_list.length).fill(0) |
75 | | - // 신고된 내용 정리할 객체 선언 |
76 | | - const obj = {} |
77 | | - // 신고된 내용 정리할 객체 사용자 아이디로 초기화 |
78 | | - id_list.forEach((id) => { |
79 | | - obj[id] = new Set() |
80 | | - }) |
81 | | - // 신고 리스트를 조회하며 신고 내용 객체에 정리 |
82 | | - // set.add 를 활용해 같은 사용자의 중복 신고 제거 |
83 | | - report.forEach((str) => { |
84 | | - const [reporter, target] = str.split(" ") |
85 | | - obj[target].add(reporter) |
86 | | - }) |
87 | | - // obj value 하나씩 접근 |
88 | | - for (const value of Object.values(obj)) { |
89 | | - // 신고당한 횟수가 정지 기준에 적합하다면 |
90 | | - if (value.size >= k) { |
91 | | - // 해당 유저를 신고한 사람들 |
92 | | - for (let item of value) { |
93 | | - // 인덱스 구해서 증가 |
94 | | - let index = id_list.indexOf(item) |
95 | | - answer[index]++ |
96 | | - } |
| 73 | + // 사용자 길이의 빈 배열을 만든다 0 |
| 74 | + var answer = new Array(id_list.length).fill(0); |
| 75 | + // 신고된 내용 정리할 객체 선언 |
| 76 | + const obj = {}; |
| 77 | + // 신고된 내용 정리할 객체 사용자 아이디로 초기화 |
| 78 | + id_list.forEach((id) => { |
| 79 | + obj[id] = new Set(); |
| 80 | + }); |
| 81 | + // 신고 리스트를 조회하며 신고 내용 객체에 정리 |
| 82 | + // set.add 를 활용해 같은 사용자의 중복 신고 제거 |
| 83 | + report.forEach((str) => { |
| 84 | + const [reporter, target] = str.split(" "); |
| 85 | + obj[target].add(reporter); |
| 86 | + }); |
| 87 | + // obj value 하나씩 접근 |
| 88 | + for (const value of Object.values(obj)) { |
| 89 | + // 신고당한 횟수가 정지 기준에 적합하다면 |
| 90 | + if (value.size >= k) { |
| 91 | + // 해당 유저를 신고한 사람들 |
| 92 | + for (let item of value) { |
| 93 | + // 인덱스 구해서 증가 |
| 94 | + let index = id_list.indexOf(item); |
| 95 | + answer[index]++; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return answer; |
| 101 | +} |
| 102 | + |
| 103 | +//정답 4 - prove-ability |
| 104 | +function solution(id_list, report, k) { |
| 105 | + // 사원 리스트 0으로 초기화 - 받은 메일 숫자 세기 |
| 106 | + var answer = Array.from({ length: id_list.length }).fill(0); |
| 107 | + // 신고 내용 객체 초기화 - 신고된 내용 정리 |
| 108 | + const reportObj = {}; |
| 109 | + // 신고건 반복문으로 하나씩 접근 |
| 110 | + for (let i = 0, len = report.length; i < len; i++) { |
| 111 | + const [userId, reportUserId] = report[i].split(" "); |
| 112 | + // 신고 내용 객체에 신고당한 사람이 있고 이전에 신고하지 않았다면 추가 |
| 113 | + if (reportObj[reportUserId]) { |
| 114 | + if (!reportObj[reportUserId].includes(userId)) { |
| 115 | + reportObj[reportUserId] = [...reportObj[reportUserId], userId]; |
| 116 | + } |
| 117 | + // 신고 내용 객체에 신고당한 사람이 없다면 추가 |
| 118 | + } else reportObj[reportUserId] = [userId]; |
| 119 | + } |
| 120 | + // 만들어진 신고 내용 객체로 반복적으로 접근 |
| 121 | + for (const item of Object.values(reportObj) |
| 122 | + .filter((v) => v.length >= k) |
| 123 | + .flatMap((v) => v)) { |
| 124 | + // 인덱스로 접근해 메일 카운트 증가 |
| 125 | + const index = id_list.findIndex((v) => v === item); |
| 126 | + answer[index]++; |
97 | 127 | }
|
98 | | - } |
99 | 128 |
|
100 | | - return answer |
| 129 | + return answer; |
101 | 130 | }
|
0 commit comments