[프로그래머스] 2022 KAKAO BLIND RECRUITMENT 신고 결과 받기 자바 코드

mango·2023년 3월 8일
0

* Things I learnt

1. 또 HashMap............

HashMap은 Key로 찾는 database 같다. 그리고 Array나 ArrayList와 달리 Key로 찾으니 시간효율성이 매우 좋음

* 알고리즘

  1. 신고받은 횟수, 메일받은 횟수는 HashMap으로 선언
  2. report를 하나하나 읽으면서 신고 받은 횟수를 저장함과 동시에 중복신고여부를 체크해야 하므로 (신고자 신고받은자)를 HashMap에 저장
  3. report를 하나하나 읽으면서 뒷 캐릭터(신고받은자)의 신고받은 횟수가 k보다 크면, 앞 캐릭터(신고한자)의 메일 받은 횟수를 저장
  4. 메일받은 횟수를 id_list 순서대로 answer에 저장

* 자바 코드

- (첫번째 시도)

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = {};
        
        HashMap <String, ArrayList<String>> singoWho = new HashMap <String, ArrayList<String>>();
        HashMap <String, Integer> singoGetCount = new HashMap <String, Integer>();
        HashMap <String, Integer> mailGetCount = new HashMap <String, Integer>();
        HashMap <String, Integer> IsItNew = new HashMap <String, Integer>();

        // 신고받은 횟수, 메일받은 횟수 캐릭터별 0으로 초기화
        for (int i = 0; i < id_list.length; i++){
            singoGetCount.put(id_list[i], 0);
            mailGetCount.put(id_list[i], 0);            
        }
            
        // report에 뒷 캐릭터의 신고받은 횟수 +1
        for (int i = 0; i < report.length; i++){
            if (IsItNew.get(report[i]) == null){
                String[] a = report[i].split(" ");

                int count = singoGetCount.get(a[1]);
                count++; 
                singoGetCount.put(a[1], count);
                // System.out.println(a[1] + " : " + count);
            }
            IsItNew.put(report[i], 1);            
        }
        
        // report에 뒷 캐릭터의 신고받은 횟수가 k보다 크면, 앞 캐릭터의 메일받은 횟수 +1
        for (int i = 0; i < report.length; i++){
            String[] a = report[i].split(" ");
            
            if(singoGetCount.get(a[1]) >= k){
                int count = mailGetCount.get(a[0]);
                count++; 
                mailGetCount.put(a[0], count);
            }
        }
        
        //id_list 순서대로 answer에 메일받은 횟수 넣기
        answer = new int[id_list.length];
        for (int i = 0; i < id_list.length; i++){
            answer[i] = mailGetCount.get(id_list[i]);
        }
        
        return answer;
    }
}

결과는 처참...

-> 아예 답이 틀리게 나왔다. 알고보니 메일받은 횟수를 세는 for 문에서 오류가 있었던 것(중복 신고 여부를 확인하지 않음)

- (두번째 시도)

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = {};
        
        HashMap <String, Integer> singoGetCount = new HashMap <String, Integer>();
        HashMap <String, Integer> mailGetCount = new HashMap <String, Integer>();
        HashMap <String, Integer> IsItNew = new HashMap <String, Integer>();

        // 신고받은 횟수, 메일받은 횟수 캐릭터별 0으로 초기화
        for (int i = 0; i < id_list.length; i++){
            singoGetCount.put(id_list[i], 0);
            mailGetCount.put(id_list[i], 0);            
        }
            
        // report에 뒷 캐릭터의 신고받은 횟수 +1
        for (int i = 0; i < report.length; i++){
            if (IsItNew.get(report[i]) == null){
                String[] a = report[i].split(" ");

                int count = singoGetCount.get(a[1]);
                count++; 
                singoGetCount.put(a[1], count);
                 // System.out.println(a[1] + " : " + count);
            }
            IsItNew.put(report[i], 1);            
        }
        IsItNew.clear();
        
        // report에 뒷 캐릭터의 신고받은 횟수가 k보다 크면, 앞 캐릭터의 메일받은 횟수 +1
        for (int i = 0; i < report.length; i++){
            if (IsItNew.get(report[i]) == null){
                String[] a = report[i].split(" ");

                if(singoGetCount.get(a[1]) >= k){
                    int count = mailGetCount.get(a[0]);
                    count++; 
                    mailGetCount.put(a[0], count);
                }
            }
            IsItNew.put(report[i], 1);                        
        }
        
        //id_list 순서대로 answer에 메일받은 횟수 넣기
        answer = new int[id_list.length];
        for (int i = 0; i < id_list.length; i++){
            answer[i] = mailGetCount.get(id_list[i]);
        }
        
        return answer;
    }
}

메일 받은 횟수를 세는 for문에서도 중복 신고 여부를 확인했더니 깔끔하게 성공~ 실력이 늘어가는게 보인닷!
이제 레벨2로 점푸할 시간

profile
앎의 즐거움을 아는 나는 mango ♪

0개의 댓글