https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 방법
누가(행 / from) 누구를(열 / to) 신고했는지 확인하기 위해 reportCnt라는 2차원 배열을 만들었다.
한 사람에 대해서는 한 번밖에 신고를 못하기 때문에 reportCnt[i][j]의 값이 0일 때만 1을 더해줬다.
report를 다 탐색한 이후에는 정지 당한 유저를 골라내 큐에 담고,
해당 유저를 신고한 유저를 reportCnt에서 탐색하며 신고 유저의 메일 횟수를 증가시켜주었다.
package Programmers;
import java.util.*;
class Solution_신고결과받기 {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
int[][] reportCnt = new int[id_list.length][id_list.length];
StringTokenizer st;
for (int i = 0; i < report.length; i++) {
st = new StringTokenizer(report[i], " ");
int from = matchId(id_list, st.nextToken());
int to = matchId(id_list, st.nextToken());
if (reportCnt[from][to] == 0) {
reportCnt[from][to]++;
}
}
Queue<Integer> out = new ArrayDeque<>(); // 정지 당한 유저 목록
for (int i = 0; i < id_list.length; i++) {
int cnt = 0;
for (int j = 0; j < id_list.length; j++) {
cnt += reportCnt[j][i];
}
if (cnt >= k) {
out.add(i);
}
}
while (!out.isEmpty()) {
int userIndex = out.poll();
for (int i = 0; i < id_list.length; i++) {
if (reportCnt[i][userIndex] != 0) {
answer[i]++;
}
}
}
return answer;
}
/**
* name과 일치하는 id_list의 인덱스 반환
*/
public int matchId(String[] id_list, String name) {
for (int i = 0; i < id_list.length; i++) {
if (id_list[i].equals(name)) {
return i;
}
}
return -1;
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] k진수에서 소수 개수 구하기 (Java/자바) (0) | 2022.07.07 |
---|---|
[프로그래머스] 기능개발 (Java/자바) (0) | 2022.07.04 |
[프로그래머스] 124 나라의 숫자 (Java/자바) (0) | 2022.07.04 |
[프로그래머스] 오픈채팅방 (0) | 2022.07.01 |
[프로그래머스] 문자열 압축 (Java/자바) (0) | 2022.07.01 |