본문 바로가기
알고리즘/해시

[프로그래머스] 신고 결과 받기 #Java

by VIPeveloper 2022. 5. 20.
반응형

개선 전 코드(정답이긴 함)

import java.io.IOException;
import java.util.*;

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

        for (int i : solution(id_list, report, k)) {
            System.out.printf("%d ", i);
        }
    }

    public static int [] solution(String[] id_list, String[] report, int k) {
        HashMap<String,Integer> idxMap = new HashMap<>();   // 아이디, 배열 인덱스
        HashMap<String,String> hashMap = new HashMap<>();   // 아이디, 신고한사람들
        HashMap<String,Integer> cnt = new HashMap<>();      // 신고당한아이디 , 신고당한횟수
        ArrayList<String> stopUser = new ArrayList<>();     // 정지유져

        for (int i = 0; i < id_list.length; i++) {
            idxMap.put(id_list[i],i);
        }

        // 신고 현황 파악
        for (int i = 0; i < report.length; i++) {
            String a = report[i].split(" ")[0];
            String b = report[i].split(" ")[1];

            if(hashMap.get(a)==null){
                hashMap.put(a,b);
            }else{
                String s = hashMap.get(a);
                hashMap.put(a,s+","+b);
            }
        }

        // 신고 당한 아이디랑 횟수 파악
        for(Map.Entry<String, String> s :hashMap.entrySet()){
            String key = s.getKey();
            String[] values = s.getValue().split(",");
            Set<String> tmp = new HashSet<>();
            for (int i = 0; i < values.length; i++) {
                tmp.add(values[i]);
            }
            for (String t : tmp) {
                cnt.put(t,cnt.getOrDefault(t,0)+1);
            }
        }

        // 정지유져
        for(Map.Entry<String,Integer> s2 : cnt.entrySet()){
            String key1 = s2.getKey();
            int value1 = s2.getValue();
            if(value1>=k){
                stopUser.add(key1);
            }
        }

        int [] answer = new int[id_list.length];
        for(Map.Entry<String, String> s :hashMap.entrySet()){
            String key = s.getKey();
            String[] values = s.getValue().split(",");
            Set<String> tmp = new HashSet<>();
            for (int i = 0; i < values.length; i++) {
                tmp.add(values[i]);
            }
            for (String stop : stopUser) {
                if(tmp.contains(stop)){
                    answer[idxMap.get(key)]++;
                }
            }
        }
        return answer;
    }
}
class Main {
    public static void main(String[] args) throws IOException {
        String []  id_list = {"muzi", "frodo", "apeach", "neo"};
        String [] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
        int k = 2;

        for (int i : solution(id_list, report, k)) {
            System.out.printf("%d ", i);
        }
    }

    public static int [] solution(String[] id_list, String[] report, int k) {
        HashMap<String,Integer> idxMap = new HashMap<>();   // 아이디, 배열 인덱스
        HashMap<String,HashSet<String>> hashMap = new HashMap<>();   // 아이디, 신고한사람들 리스트
        HashMap<String,Integer> cnt = new HashMap<>();      // 신고당한아이디 , 신고당한횟수
        ArrayList<String> stopUser = new ArrayList<>();     // 정지유져

        for (int i = 0; i < id_list.length; i++) {
            idxMap.put(id_list[i],i);
        }

        // 신고 현황 파악
        for (int i = 0; i < report.length; i++) {
            String a = report[i].split(" ")[0];
            String b = report[i].split(" ")[1];

            hashMap.computeIfAbsent(a, k1 -> new HashSet<>());
            HashSet<String> strings = hashMap.get(a);
            strings.add(b);
            hashMap.put(a, strings);
        }

        // 신고 당한 아이디랑 횟수 파악
        for(Map.Entry<String, HashSet<String>> s :hashMap.entrySet()){
            String key = s.getKey();
            HashSet<String> value = s.getValue();
            for (String t : value) {
                cnt.put(t,cnt.getOrDefault(t,0)+1);
            }
        }

        // 정지유져
        for(Map.Entry<String,Integer> s2 : cnt.entrySet()){
            String key1 = s2.getKey();
            int value1 = s2.getValue();
            if(value1>=k){
                stopUser.add(key1);
            }
        }

        int [] answer = new int[id_list.length];
        for(Map.Entry<String, HashSet<String>> s : hashMap.entrySet()){
            String key = s.getKey();
            HashSet<String> value = s.getValue();
            for (String stop : stopUser) {
                if(value.contains(stop)){
                    answer[idxMap.get(key)]++;
                }
            }
        }
        return answer;
    }
}
반응형