코딩테스트

[ 프로그래머스 ] 신고 결과 받기 - java

Adose 2025. 3. 20. 19:33

📘요구 사항 정리

  • 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다.
  • 무지가 개발하려는 시스템은 다음과 같습니다.
    • 각 유저는 한 번에 한 명의 유저를 신고할 수 있습니다.
      • 신고 횟수에 제한은 없습니다. 서로 다른 유저를 계속해서 신고할 수 있습니다.
      • 한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다.
    • k번 이상 신고된 유저는 게시판 이용이 정지되며, 해당 유저를 신고한 모든 유저에게 정지 사실을 메일로 발송합니다.
      • 유저가 신고한 모든 내용을 취합하여 마지막에 한꺼번에 게시판 이용 정지를 시키면서 정지 메일을 발송합니다.
  • 다음은 전체 유저 목록이 ["muzi", "frodo", "apeach", "neo"]이고, k = 2(즉, 2번 이상 신고당하면 이용 정지)인 경우의 예시입니다.
  • 유저 ID 유저가 신고한 ID 설명
    "muzi" "frodo" "muzi"가 "frodo"를 신고했습니다.
    "apeach" "frodo" "apeach"가 "frodo"를 신고했습니다.
    "frodo" "neo" "frodo"가 "neo"를 신고했습니다.
    "muzi" "neo" "muzi"가 "neo"를 신고했습니다.
    "apeach" "muzi" "apeach"가 "muzi"를 신고했습니다.
  • 각 유저별로 신고당한 횟수는 다음과 같습니다.유저 ID 신고당한 횟수
    "muzi" 1
    "frodo" 2
    "apeach" 0
    "neo" 2
  • 위 예시에서는 2번 이상 신고당한 "frodo"와 "neo"의 게시판 이용이 정지됩니다. 이때, 각 유저별로 신고한 아이디와 정지된 아이디를 정리하면 다음과 같습니다.유저 ID 유저가 신고한 ID 정지된 ID
    "muzi" ["frodo", "neo"] ["frodo", "neo"]
    "frodo" ["neo"] ["neo"]
    "apeach" ["muzi", "frodo"] ["frodo"]
    "neo" 없음 없음
  • 따라서 "muzi"는 처리 결과 메일을 2회, "frodo"와 "apeach"는 각각 처리 결과 메일을 1회 받게 됩니다.

 

이용자의 ID가 담긴 문자열 배열 id_list, 각 이용자가 신고한 이용자의 ID 정보가 담긴 문자열 배열 report, 정지 기준이 되는 신고 횟수 k가 매개변수로 주어질 때, 각 유저별로 처리 결과 메일을 받은 횟수를 배열에 담아 return 하도록 solution 함수를 완성해주세요.

 

📌 제약 조건

  • 2 ≤ id_list의 길이 ≤ 1,000
    • 1 ≤ id_list의 원소 길이 ≤ 10
    • id_list의 원소는 이용자의 id를 나타내는 문자열이며 알파벳 소문자로만 이루어져 있습니다.
    • id_list에는 같은 아이디가 중복해서 들어있지 않습니다.
  • 1 ≤ report의 길이 ≤ 200,000
    • 3 ≤ report의 원소 길이 ≤ 21
    • report의 원소는 "이용자id 신고한id"형태의 문자열입니다.
    • 예를 들어 "muzi frodo"의 경우 "muzi"가 "frodo"를 신고했다는 의미입니다.
    • id는 알파벳 소문자로만 이루어져 있습니다.
    • 이용자id와 신고한id는 공백(스페이스)하나로 구분되어 있습니다.
    • 자기 자신을 신고하는 경우는 없습니다.
  • 1 ≤ k ≤ 200, k는 자연수입니다.
  • return 하는 배열은 id_list에 담긴 id 순서대로 각 유저가 받은 결과 메일 수를 담으면 됩니다.

 

📌 입출력 예

id_list report k result

["muzi", "frodo", "apeach", "neo"] ["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"] 2 [2,1,1,0]
["con", "ryan"] ["ryan con", "ryan con", "ryan con", "ryan con"] 3 [0,0]

 

📌 구현코드 -1

 public static int[] solution(String[] id_list, String[] report, int k) {
        HashSet<String> banUser = new HashSet<>();
        HashMap<String,HashSet<String>> userReportCount = new HashMap<>();
        HashMap<String,HashSet<String>> banUserCount = new HashMap<>();

        //유저별 신고당한 횟수
        //본인
        //본인이 신고한 애
        for(String content  : report){
            String[] reportContent = content.split(" ");
            //userReportCount.put(reportContent[1],userReportCount.getOrDefault(reportContent[1],0)+1);
            if(!userReportCount.containsKey(reportContent[1])){
                HashSet<String> user = new HashSet<>();
                user.add(reportContent[0]);
                userReportCount.put(reportContent[1],user);
            }else{
                HashSet<String> user = userReportCount.get(reportContent[1]);
                user.add(reportContent[0]);
                userReportCount.put(reportContent[1],user);
            }
        }

        //정지된 아이디
        for(String name : userReportCount.keySet()){
            if(userReportCount.get(name).size()>=k){
                banUser.add(name);
            }
        }

        //0은 본인
        //1은 본인이 신고한 애
        for(String content : report){
            String[] reportContent = content.split(" ");
            //banUser에 들어가 있으면,
            if(banUser.contains(reportContent[1])) {
                if (!banUserCount.containsKey(reportContent[0])) {
                    HashSet<String> banData = new HashSet<>();
                    banData.add(reportContent[1]);
                    banUserCount.put(reportContent[0], banData);
                } else {
                    HashSet<String> banData = banUserCount.get(reportContent[0]);
                    banData.add(reportContent[1]);
                    banUserCount.put(reportContent[0], banData);
                }
            }
        }

        //결과
        int[] answer = new int [id_list.length];

        for(int i=0;i<id_list.length;i++){
            if(banUserCount.containsKey(id_list[i])){
                answer[i] = banUserCount.get(id_list[i]).size();
            }else{
                answer[i] = 0;
            }
        }

        return answer;
    }

 

✅  구현코드 해설 -1

결론은 본인이 신고한 아이디인 사람이, 정지가 되었는가, 몇명 정지가 되었는가 필요

  • 이때 정지 기준이 되는 신고 횟수를 초과한 사람많이 정지 된다.

  • 정지된 아이디를 따로 관리해야 한다. → 배열 필요
    • HashSet<String> banUser
  • 저별 신고당한 횟수의 → 배열이 필요
    • HashMap<String아이디, HashSet<String> > userReportCount
    • HashSet<String> → 신고당한 사람이 들어간다. 한명이 한번만 신고할 수 있기 때문에 중복 값을 방지하기 위해 Set을 사용하였다.
  • 본인이 신고한 아이디인 사람이 정지 되었는가 몇명 정지 - > 배열 필요
    • HashMap<String(아이디), Integer(몇명정지 구하는 횟수)> banUserCount
  • return할때, id_list for문을 돌아서,
    • i번째 배열에 있는 아이디 값을 가져와서, map에서 get으로 값을 얻어낸 후에
    • 배열에 넣어줘서 return하면 된다.

 

 

📌 구현코드 - 2

  • 첫번째 구현 코드에서 banUser가 굳이 필요 없을 것 같아서, 제외하고 다시 코드를 구현해보았다.
    public static int[] solution(String[] id_list, String[] report, int k) {
        HashMap<String,HashSet<String>> userReportCount = new HashMap<>();
        HashMap<String,HashSet<String>> banUserCount = new HashMap<>();


        //유저별 신고당한 횟수
        //본인
        //본인이 신고한 애
        for(String content  : report){
            String[] reportContent = content.split(" ");
            //userReportCount.put(reportContent[1],userReportCount.getOrDefault(reportContent[1],0)+1);
            if(!userReportCount.containsKey(reportContent[1])){
                HashSet<String> user = new HashSet<>();
                user.add(reportContent[0]);
                userReportCount.put(reportContent[1],user);
            }else{
                HashSet<String> user = userReportCount.get(reportContent[1]);
                user.add(reportContent[0]);
                userReportCount.put(reportContent[1],user);
            }
        }


        //0은 본인
        //1은 본인이 신고한 애
        for(String content : report){
            String[] reportContent = content.split(" ");
            //banUser에 들어가 있으면,

            //바로 userReportCount.get size로 할 수 있지 않나 ?

            if(userReportCount.get(reportContent[1]).size()>=k){
                if(!banUserCount.containsKey(reportContent[0])){
                HashSet<String> banData = new HashSet<>();
                banData.add(reportContent[1]);
                banUserCount.put(reportContent[0], banData);
                }else{
                    HashSet<String> banData = banUserCount.get(reportContent[0]);
                    banData.add(reportContent[1]);
                    banUserCount.put(reportContent[0], banData);
                }
            }
        }

        //결과
        int[] answer = new int [id_list.length];

        for(int i=0;i<id_list.length;i++){
            if(banUserCount.containsKey(id_list[i])){
                answer[i] = banUserCount.get(id_list[i]).size();
            }else{
                answer[i] = 0;
            }
        }


        return answer;
    }

 

✅  구현코드 해설 -2

  • 첫번째 코드에서 쓰였던 banUser를 제외하고 구현하였다.
  • 마지막 신고 횟수를 계산하면 바로 결과를 도출해낼 수 있다.