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

: ) YOUNG·2024년 1월 8일
1

알고리즘

목록 보기
291/370
post-thumbnail

프로그래머스 92334번
https://school.programmers.co.kr/learn/courses/30/lessons/92334

문제



생각하기


  • 문자열 문제인데 HashMap을 사용해서 풀수있다.

    • 문자열 + 자료구조 문제인 것 같음

동작

신고당한 사람의 ID를 key값으로 하고, 신고한 사람의 목록을 List<String>으로 받아서 HashMap으로 만들어서,
신고당한 사람과 신고한 사람의 목록을 만든다.



        for(String id : reportMap.keySet()) {
            if(reportMap.get(id).size() < k) continue;
                        
            for(int i=0; i<id_list.length; i++) {
                String name = id_list[i];
                
                if(reportMap.get(id).contains(name)) {
                    ans[i]++;
                }
            }


결과


코드



import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] ans = new int[id_list.length];
        
        // 신고당한 ID, 신고 당한 횟수
        HashMap<String, List<String>> reportMap = new HashMap<>();

        int len = report.length;
        for(int i=0; i<len; i++) {
            StringTokenizer st = new StringTokenizer(report[i]);     
            String temp1 = st.nextToken(); // 신고한 사람
            String temp2 = st.nextToken(); // 신고당한 사람
            
            if(reportMap.get(temp2) == null) {
                List<String> tempList = new ArrayList<>();
                tempList.add(temp1); 
                reportMap.put(temp2, tempList);
            } else {
                List<String> tempList = reportMap.get(temp2);
                
                if(tempList.contains(temp1)) continue;
                
                tempList.add(temp1);
                reportMap.remove(temp2);
                reportMap.put(temp2, tempList);
            }
        }
                
        for(String id : reportMap.keySet()) {
            if(reportMap.get(id).size() < k) continue;
                        
            for(int i=0; i<id_list.length; i++) {
                String name = id_list[i];
                
                if(reportMap.get(id).contains(name)) {
                    ans[i]++;
                }
            }
        }
        
        
        return ans;
    } // End of solution()
} // End of Solution class

0개의 댓글