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 ) => { //Initialization
14
- reportedCount [ element ] = 0
15
- mailCount [ element ] = 0
16
- reportedBy [ element ] = [ ]
17
- } )
18
- reportSet . forEach ( ( element ) => {
19
- const $id = ( element . split ( " " ) ) [ 0 ]
20
- const $reported = ( element . split ( " " ) ) [ 1 ]
21
- reportedCount [ $reported ] += 1
22
- reportedBy [ $reported ] . push ( $id )
23
- } )
24
- for ( const reportedId in reportedCount ) {
25
- if ( reportedCount [ reportedId ] >= k ) {
26
- reportedBy [ reportedId ] . forEach ( ( reporter ) => {
27
- mailCount [ reporter ] += 1 ;
28
- } )
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
- var answer = [ ]
32
- id_list . forEach ( ( element ) => {
33
- answer . push ( mailCount [ element ] )
34
- } )
31
+ }
32
+ var answer = [ ]
33
+ id_list . forEach ( ( element ) => {
34
+ answer . push ( mailCount [ element ] )
35
+ } )
35
36
36
- return answer
37
+ return answer
37
38
}
38
39
39
40
//정답 2 - jaewon1676
40
41
function solution ( id_list , report , k ) {
41
-
42
- let answer = new Array ( id_list . length ) . fill ( 0 )
43
- let report_list = { } // 신고당한 ID
44
-
45
- // key, value 형식의 report_list 객체를 만든다.
46
- id_list . map ( ( user ) => {
42
+ let answer = new Array ( id_list . length ) . fill ( 0 )
43
+ let report_list = { } // 신고당한 ID
44
+
45
+ // key, value 형식의 report_list 객체를 만든다.
46
+ id_list . map ( ( user ) => {
47
47
report_list [ user ] = [ ] //key = userid , value = 빈 배열을 가지는 객체
48
- } )
49
- // report_list { muzi: [], frodo: [], apeach: [], neo: [] }
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
- // 배열에 포함하는지 여부를 확인하여 포함하지 않을때 신고자의 이름을 추가하였다.
58
- }
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
+ }
67
+ }
68
+ return answer
69
+ }
59
70
60
- } )
61
- for ( const key in report_list ) { // 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
- }
71
+ //정답 3 - prove-ability
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
+ }
67
97
}
68
- return answer
69
- }
98
+ }
99
+
100
+ return answer
101
+ }
0 commit comments