알고리즘
프로그래머스 신고 결과 받기 (JAVA)
박카스마시며코딩
2023. 8. 8. 13:11
https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
저는 구현을 통해 문제를 해결하였습니다.
id_list를 통해 각 이름에 따른 번호를 매기고, 2차원 배열을 통해 누가 누구를 신고하였는지를 저장하였습니다.
2차원 배열을 돌면서 한 사람이 얼마나 신고당했는지를 확인하고 이 값이 k보다 큰 사람을 체크하고 이를 신고한 사람의 수를 체크하여 문제를 해결하였습니다.
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int size = id_list.length;
int[] answer = new int[size];
boolean[][] reportArr = new boolean[size][size];
Map<String,Integer> map = makeMap(id_list);
for(String str : report){
String[] temp = str.split(" ");
int first = map.get(temp[0]);
int second = map.get(temp[1]);
reportArr[first][second] = true;
}
boolean[] isBlock = new boolean[size];
for(int i = 0 ; i < size ; i++){
int cnt = 0;
for(int j = 0 ; j < size ; j++){
if(reportArr[j][i]){
cnt++;
}
}
if(cnt >= k){
isBlock[i] = true;
}
}
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j < size ; j++){
if(isBlock[j] && reportArr[i][j]){
answer[i]++;
}
}
}
return answer;
}
private static Map<String,Integer> makeMap(String[] id_list){
Map<String,Integer> map = new HashMap<>();
for(int i = 0 ; i < id_list.length ; i++){
map.put(id_list[i],i);
}
return map;
}
}