Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 신고 결과 받기 본문

Java

프로그래머스: 신고 결과 받기

Daeryuk Kim 2024. 1. 20. 16:13
import java.util.*;

class User{
    String name;                    //해당 유저명
    HashSet<String> reportList;		//피신고자 리스트
    HashSet<String> reportedList;	//신고자 리스트
    public User(String name){
        this.name = name;
        reportList = new HashSet<>();
        reportedList = new HashSet<>();
    }
}
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        ArrayList<User> users = new ArrayList<>();
        HashMap<String,Integer> suspendedList = new HashMap<>();
        HashMap<String,Integer> idIdx = new HashMap<String,Integer>();
        int idx = 0;
        
        //각 유저에 대한 고유 index를 setting후 users ArrayList에 User 객체 추가
        for(String name : id_list) {
            idIdx.put(name, idx++);
            users.add(new User(name));
        }
        
        //users의 idIdx.get(str[0])번째 index에 reportList에 피신고자 추가
        //users의 idIdx.get(str[1])번째 index에 reportedList에 신고자 추가
        for(String re : report){
            String[] str = re.split(" ");
            users.get(idIdx.get(str[0])).reportList.add(str[1]);
            users.get(idIdx.get(str[1])).reportedList.add(str[0]);
        }
        
        //reportedList의 크기가 주어진 k와 같거나 크면 suspendedList의 피신고자 put
        for(User user : users){
            if(user.reportedList.size() >= k)
                suspendedList.put(user.name,1);
        }
        
        //answer 배열 각 index에 해당하는 유저의 신고당한 횟수를 count up
        for(User user : users){
             for(String nameReport : user.reportList){
                 if(suspendedList.get(nameReport) != null){
                     answer[idIdx.get(user.name)]++;
                 }
             }
        }
        return answer;
    }

    public static void main(String[] args) {
        
        /*
            "muzi"	 1
            "frodo"	 2  => 응~ 밴
            "apeach" 0
            "neo"	 2  => 응~ 밴   
        */
        String[] id_list = {"muzi", "frodo", "apeach", "neo"};
        String[] report = {
            "muzi frodo",
            "apeach frodo",
            "frodo neo",
            "muzi neo",
            "apeach muzi"
        };
        int k = 2; // 2번 이상이 신고당하면 밴 당함
        
        // "처리 되었습니다." 라는 결과 메일을 몇 번 보내줘야 하는가?
        Solution sol = new Solution();
        int[] result = sol.solution(id_list, report, k);
        for (int i: result) {
            System.out.print(i+" "); 
        }
        
    }
}

위의 코드는 내가 짠 건 아니다. 이해는 했는데 코드로 짜기가 너무 어렵다. ㅠㅠ

객체지향까지 챙기면서 기능까지 가져간 이 코드를 보고 감탄을 금치 못했다.

아예 처음부터 User 클래스를 생성하여 이름, 피신고자list , 신고자list 까지 한 번에 관리할 수 있다.

난 갈 길이 너무 멀다.