알고리즘/프로그래머스
[Java] 신고 결과 받기 - Kakao 2022 Blind Recruitment
Garonguri
2022. 3. 12. 01:06
728x90
https://programmers.co.kr/learn/courses/30/lessons/92334?language=java
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
import java.util.*;
public class pg신고결과받기 {
public static void main(String[] args) {
String[] id_list = {"con", "ryan"};
String[] report = {"ryan con", "ryan con", "ryan con", "ryan con"};
int k =3;
System.out.println(Arrays.toString(solution(id_list, report, k)));
}
public static int[] solution(String[] id_list, String[] report, int k) {
StringTokenizer st;
HashSet<Index> hashSet = new HashSet<>();
int[] answer = new int[id_list.length];
ArrayList<Kakao> kakaos = new ArrayList<>();
int[][] singo = new int[id_list.length][id_list.length];
//카카오 애들 입력
for(int i=0;i< id_list.length;i++){
kakaos.add(new Kakao(id_list[i], i, 0, false));
//System.out.println(kakaos.get(i));
}
for(int i=0;i<report.length;i++){
st = new StringTokenizer(report[i]);
String from = st.nextToken();
String to = st.nextToken();
//from이 to를 신고했음
int fromidx = kakaos.indexOf(new Kakao(from));
int toidx = kakaos.indexOf(new Kakao(to));
Index tmp = new Index(fromidx, toidx);
if(hashSet.contains(tmp)){
continue;
}else{
hashSet.add(tmp);
singo[fromidx][toidx]+=1;
kakaos.get(toidx).cnt +=1;
}
//System.out.println(hashSet);
}
for(int i=0;i< id_list.length;i++){
if(kakaos.get(i).cnt >=k){
kakaos.get(i).bye = true;
}
}
for(int i=0;i<id_list.length;i++){
for(int j=0;j<id_list.length;j++){
if(singo[i][j] >0 && kakaos.get(j).bye){
answer[i] +=1;
}
}
}
return answer;
}
public static class Index{
int from;
int to;
public Index(int from, int to){
this.from = from;
this.to = to;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Index index = (Index) o;
return from == index.from && to == index.to;
}
@Override
public int hashCode() {
return Objects.hash(from, to);
}
@Override
public String toString() {
return "Index{" +
"from=" + from +
", to=" + to +
'}';
}
}
public static class Kakao{
String name;
int idx;
int cnt;
boolean bye;
public Kakao(String name) {
this.name = name;
}
public Kakao(String name, int idx, int cnt, boolean bye) {
this.name = name;
this.idx = idx;
this.cnt = cnt;
this.bye = bye;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Kakao kakao = (Kakao) o;
return name.equals(kakao.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "Kakao{" +
"name='" + name + '\'' +
", idx=" + idx +
", cnt=" + cnt +
'}';
}
}
}
|
cs |
[풀이]
hash set으로 같은 사람을 여러번 신고하는 반복 작업을 무시해준뒤 각 객체의 속성에 신고 횟수를 넣어 k값과 비교하였다.
내가 누굴 신고했는지는 배열을 통해 저장하고, 인덱스를 통해 비교해줬다.
생성자 재정의는 낚시왕을, hashset을 통한 방문 처리는 구슬 탈출 문제를 생각하며 풀었다.
프로그래머스도 가끔 풀어야겠다. 백준이랑 구조가 달라서 헷갈린다.
끄읕
728x90