1 분 소요

📁 [Lv1_92334] 신고 결과 받기

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class PRO_92334 {
	public static void main(String[] args) {
		String[] id_list = {"muzi", "frodo", "apeach", "neo"};
		String[] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
		int k = 2;

		int[] answer = new int[id_list.length];

		// 유저 ID / 신고한 유저 ID set
		Map<String,HashSet<String>> map = new HashMap<>();
		// 결과 Map
		Map<String,Integer> answerMap = new HashMap<>();

		// Map 초기화
		for(int i=0; i<id_list.length; i++) {
			HashSet<String> reportingId = new HashSet<>();
			map.put(id_list[i],reportingId);
			// 받은 메일수 0 개
			answerMap.put(id_list[i], 0);
		}

		// report 기록 넣기
		for(int i=0; i<report.length; i++) {
			String[] str = report[i].split(" ");
			// 신고한 ID
			String reportingId = str[0];
			// 신고 당한 ID
			String reportedId = str[1];
			// 신고된 유저가 Key 값이 되고 신고한 유저가 value 값이 된다.
			map.get(reportedId).add(reportingId);
		}

		// 결과 설정
		for(String userId : map.keySet()) {
			// 신고한 유저 HashSet
			HashSet<String> reportingSet = map.get(userId);
			// 신고 횟수가 k 이상일 경우
			if(reportingSet.size()>=k) {
				// 신고한 사람들에게 메일 보내기
				for(String temp : reportingSet) {
					answerMap.put(temp, answerMap.get(temp)+1);
				}
			}
		}

		// answer 배열에 세팅
		for(int i=0; i<answer.length;i++) {
			answer[i] = answerMap.get(id_list[i]);
		}

	}
}

🤔 나의 생각

레벨 1 카카오 문제다..
확실히 문제 난이도가 어렵진 않지만 복잡하다 ㅋㅋㅋ
생각을 원래 2번 정도만 해야한다면 이 문제는 4번정도? ㅋㅋㅋ
여기서 원래 나는 List를 사용해서 신고한 User를 표현해줬는데 이 경우 중복검사를 한 번 더 해줘야 하기 때문에 HashSet을 사용해서 해결해 주었다.
그리고 신고한 사람, 신고 당한 사람을 잘 구분해서 코드를 작성해 줘야지 헷갈리지 않고 작성할 것 같다..
확실히 복잡쓰…