백준 & 프로그래머스

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

concho 2023. 7. 15. 15:11

https://school.programmers.co.kr/learn/courses/30/lessons/92334

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1)

import java.util.*;
class Solution {
    public int[] solution(String[] idList, String[] report, int k) {
        //set으로 신고 중복 제거
        Set<String> reportSet = new HashSet<>();
        for (int i = 0; i < report.length; i++){
            reportSet.add(report[i]);
        }
        String[] reporter = reportSet.toArray(new String[0]);
        String[] reportedUser = new String[reporter.length];
        
        // 각 유저별 신고 카운트
        for (int i = 0; i < reporter.length; i++){
            String[] tm = reporter[i].split(" ");
            reporter[i] = tm[0];
            reportedUser[i] = tm[1];
        }
        
        //banUser 추출
        int[] banCnt = new int[idList.length];
        Queue<String> banUserQ = new LinkedList<>();
        for (int i = 0; i < reporter.length; i++){
            for (int j = 0; j < idList.length; j++){
                if (idList[j].equals(reportedUser[i])) {
                    banCnt[j]++;
                    if (banCnt[j] == k) {
                        banUserQ.offer(reportedUser[i]);
                        break;
                    }
                }
            }
        }
        // banUser의 신고자 파악
        Queue<String> reporterQ = new LinkedList<>();
        while(!banUserQ.isEmpty()){
            for (int i = 0; i < reportedUser.length; i++){
                if (banUserQ.peek().equals(reportedUser[i])){
                    reportedUser[i] = "";
                    reporterQ.offer(reporter[i]);
                }
            }
            banUserQ.poll();
        }
        // user별 reportMail 카운트
        int[] reporterMailCnt = new int[idList.length];
        while(!reporterQ.isEmpty()){
            for (int i = 0; i < idList.length; i++){
                if (reporterQ.peek().equals(idList[i])){
                    reporterMailCnt[i]++;
                    break;
                }
            }
            reporterQ.poll();
        }
        return reporterMailCnt;
    }
}