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

스브코·2022년 2월 4일
0

문제출처: https://programmers.co.kr/learn/courses/30/lessons/92334

문제설명


출처 참조


문제풀이

import java.util.*;
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        HashMap<String, Integer> indexStorage = new HashMap<>();
        for(int i = 0; i < id_list.length; i++) {
            indexStorage.put(id_list[i], i);
        }
        
        HashMap<String, HashSet<String>> hm = new HashMap<>();
        for(String s : report) {
            String key = s.split(" ")[0];
            String val = s.split(" ")[1];
            HashSet<String> hs;
            if(hm.containsKey(key))
                hs = hm.get(key);
            else
                hs = new HashSet<String>();
            hs.add(val);
            hm.put(key, hs);
        }
        
        HashMap<String, Integer> hm2 = new HashMap<>();
        for(String s : id_list) {
            hm2.put(s, 0);
        }

        for(String s : hm.keySet()) {
            HashSet<String> hs = hm.get(s);
            for(String hss : hs) {
                if(hm2.containsKey(hss)) {
                    hm2.put(hss, hm2.get(hss) + 1);
                }
            }
        }
        
        for(int i = 0; i < id_list.length; i++) {
            if(hm.containsKey(id_list[i])) {
                HashSet<String> hs = hm.get(id_list[i]);
                for(String s : hs) {
                    if(hm2.containsKey(s)) {
                        if(hm2.get(s) >= k) {
                            if(indexStorage.containsKey(s)) {
                                answer[indexStorage.get(id_list[i])]++;
                            }
                        }
                    }
                }
            }
        }

        return answer;
    }
}

풀이방법으로는 HashSet과 HashMap을 적극 활용하였다.

1번 해쉬맵은

key = user
value = user가 신고한 유저들이 담긴 HashSet이다. HashSet에 의해 중복 신고는 제외된다.

2번 해쉬맵은

key = user
value = 유저들 마다 신고 당한 횟수이다.

indexStrage 해쉬맵은

key = user
value = id_list 배열에 있는 user의 배열 index 번호이다.


세개의 해쉬맵을 정리한 다음,

1번 해쉬맵의 키마다 들어 있는 해쉬셋을 loop한다. 해쉬셋 안의 신고 당한 유저들이 총 몇 번 신고 당한지 확인 후 k와 비교하여 k보다 크거나 같다면 user(1번 해쉬맵 key)의 id_list 인덱스를 indexStorage에서 확인하여 증가 시킨다.

어렵진 않았으나 직관적으로 풀려다 보니 좀 시간이 소요 되었다.


다른풀이

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        List<String> list = Arrays.stream(report).distinct().collect(Collectors.toList());
        HashMap<String, Integer> count = new HashMap<>();
        for (String s : list) {
            String target = s.split(" ")[1];
            count.put(target, count.getOrDefault(target, 0) + 1);
        }

        return Arrays.stream(id_list).map(_user -> {
            final String user = _user;
            List<String> reportList = list.stream().filter(s -> s.startsWith(user + " ")).collect(Collectors.toList());
            return reportList.stream().filter(s -> count.getOrDefault(s.split(" ")[1], 0) >= k).count();
        }).mapToInt(Long::intValue).toArray();
    }
}

stream을 사용하여 풀라고 만든 문제같다... 파이썬처럼 코드가 훨씬 간결한데, 람다와 stream 함수 공부의 필요성이 많이 느껴졌다.

profile
익히는 속도가 까먹는 속도를 추월하는 그날까지...

0개의 댓글