ps

[programmers] 신고 결과 받기

수수한개발자 2022. 5. 10.
728x90

문제설명

문제풀이

import java.util.*;
class Solution {
	public int[] solution(String[] id_list, String[] report, int k) {
		int[] answer = new int[id_list.length];
		
		//1. 중복제거
		HashSet<String> reportSet = new HashSet<String>();
		for (String rep: report) {
			reportSet.add(rep);
		}
		
		//2. notifyListHash만들기
		HashMap<String, ArrayList<String>> notifyListHash = new HashMap<>();
		for(String rep : reportSet) {
			int blank = rep.indexOf(" ");
			String reporter = rep.substring(0,blank);
			String reported = rep.substring(blank+1);
			
			ArrayList<String> repoterList = notifyListHash.getOrDefault(reported, null);//null값은 신고 당한 적이 없다.
			if(repoterList == null) repoterList = new ArrayList<>();
			
			repoterList.add(reporter);
			notifyListHash.put(reported, repoterList);
			
		}
	
		//3. notifyLisyHash 기반으로 reporterHash 만들기
		HashMap<String, Integer> reporterHash = new HashMap<>();
		for(ArrayList<String> notifyList : notifyListHash.values()) {
			if(notifyList.size() >= k) {
				for(String reporter : notifyList) {
					reporterHash.put(reporter, reporterHash.getOrDefault(reporter, 0)+1);
				}
			}
		}
		//4. reporterHash 기반으로 answer 배열을 채운다.
		for(int i=0;i<id_list.length;i++) {
			answer[i] =reporterHash.getOrDefault(id_list[i], 0);
		}
		
		return answer;
	}
	
	
	public static void main(String[] args) {
		Solution sol = new Solution();
		String[] id_list = {"muzi", "frodo", "apeach", "neo"};
		String[] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
		int k = 2;
		sol.solution(id_list, report, k);
	}
}

1. 중복제거

//1. 중복제거
HashSet<String> reportSet = new HashSet<String>();
for (String rep: report) {
    reportSet.add(rep);
}

여기서 HashSet이란?

HashSet은 Set 인터페이스의 구현 클래스입니다. 그렇기에 Set의 성질을 그대로 상속받습니다. Set은 객체를 중복해서 저장할 수 없고 하나의 null 값만 저장할 수 있습니다. 또한 저장 순서가 유지되지 않습니다.

문제에 각 유저는 한 번에 한 명의 유저를 신고할 수 있습니다. 라는 조건을 만족하기 위해 중복제거를 해줍니다.

 

2. 신고 받은 유저와 신고한 유저 나누기

//2. notifyListHash만들기
HashMap<String, ArrayList<String>> notifyListHash = new HashMap<>();
for(String rep : reportSet) {
    int blank = rep.indexOf(" ");
    String reporter = rep.substring(0,blank);
    String reported = rep.substring(blank+1);

    ArrayList<String> repoterList = notifyListHash.getOrDefault(reported, null);//null값은 신고 당한 적이 없다.
    if(repoterList == null) repoterList = new ArrayList<>();

    repoterList.add(reporter);
    notifyListHash.put(reported, repoterList);

}

신고를 받은 유저와 신고한 유저를 나누기 위해 신고를 받은 유저는 String, 신고를 한 유저들을 담은 배열 ArrayList를 넣을 HashMap에 선언합니다.

그리고 위에서 중복제거한 reportSet에서 하나씩 꺼내어 공백index(blank)를 기준으로 substring을 해서 신고를 받은 유저와 신고한 유저를 파싱해줍니다.

그리고 repoterList에 신고를 한 유저들의 이름을 넣어주고 notifyListHash의 key값은 신고를 받은 유저 벨류는 신고를 한 유저들의 이름을 넣어 줍니다.

notifyListHash 결과 값

notifyListHash: {muzi=[apeach], neo=[frodo, muzi], frodo=[apeach, muzi]}

 

3.신고 받은 횟수 카운트하기

//3. notifyLisyHash 기반으로 reporterHash 만들기
HashMap<String, Integer> reporterHash = new HashMap<>();
for(ArrayList<String> notifyList : notifyListHash.values()) {
    if(notifyList.size() >= k) {
        for(String reporter : notifyList) {
            reporterHash.put(reporter, reporterHash.getOrDefault(reporter, 0)+1);
        }
    }
}

notifListHash의 벨류만(repoterList) 가져와서 notifyList에 넣어줍니다. (신고한 유저들의 이름들)

이러면 신고를 한 유저의 이름들만 notifyList에 들어갑니다.

문제조건이 밑에와 같기 때문에

k번 이상 신고된 유저는 게시판 이용이 정지되며, 해당 유저를 신고한 모든 유저에게 정지 사실을 메일로 발송합니다.

notifyList의 size() 가 k 이상이면 신고한 사람이 알림을 받아야 합니다.

이때 새로운 해쉬에 추가를 하는데 getOrDefault 메소드를 이용하여 HashMap에 reporter가 있으면 매핑되어 있는 값을 반환하고 없으면 defalut==0 값을 반환하게 해주면 

처음 frodo를 신고한 notifyList 에는 muzi, apeach 가 있습니다. 이러면 둘다 reportHash에 처음에는 없기때문에 

0이 들어가지만 뒤에 +1을 해줘서 1이들어가고 

두번째 neo를 신고한 notifyList 에는 frodo, muzi가 있습니다. muzi는 이미 reportHash 에 있기 때문에 1을 반환해 1+1이 되어 2가 됩니다.

 

4. 신고한 사용자에게 알림

//4. reporterHash 기반으로 answer 배열을 채운다.
for(int i=0;i<id_list.length;i++) {
    answer[i] =reporterHash.getOrDefault(id_list[i], 0);
}

id_list : 신고한 유저들의 이름이 담긴 배열

위의 reportHash 의 신고한 유저들의 이름이 있고 벨류 값으로 몇번신고 했는지 들어있기때문에 answer에 reportHash 에 매핑된 값만 넘겨주면 됩니다.

 

문제출처

https://programmers.co.kr/learn/courses/30/lessons/92334

 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

 

728x90

'ps' 카테고리의 다른 글

[알고리즘] 문자 찾기  (0) 2022.06.27
[programmers] 숫자 문자열과 영단어  (0) 2022.05.10
[programmers] 신규 아이디 추천  (2) 2022.05.10
[programmers] 하샤드 수  (0) 2022.05.09
상위 n개 레코드  (0) 2022.05.09

댓글