been_dev
been_archive
been_dev
전체 방문자
오늘
어제
  • 분류 전체보기 (34)
    • f-lab (3)
    • project (2)
    • solve (29)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 완전탐색
  • 에프랩 2개월 후기
  • 프로그래머스
  • 백준
  • f-lab 2개월 후기
  • Downloading from external resources is disabled
  • 자바
  • JWT
  • 코딩테스트
  • 이진변환반복하기
  • 해시
  • Lombok
  • specify location
  • Eclipse
  • 숫자의표현
  • 에프랩 1개월 후기
  • 문자열
  • f-lab 1개월 후기
  • 큐
  • 자바 백엔드
  • 탐욕법
  • 버튼미노출
  • 후기
  • jadencase만들기
  • 그리디
  • MYSQL
  • 실행창 작음
  • 스택
  • 에프랩
  • f-lab

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
been_dev

been_archive

solve

[프로그래머스/해시] Lv1. 신고 결과 받기(자바)

2024. 1. 11. 22:46

 

해시 

import java.util.*;

// 1. 신고 내역을 set 으로 만든다.
// 2. 신고 결과 내역을 map 으로 만든다. (신고 대상 아이디: 신고한 사람 아이디 목록)
// 3. 정지된 계정을 찾는다.
// 4. 해당 계정을 신고한 아이디 목록을 map 으로 정리 (신고한 사람 아이디: count)

class Solution {
    
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        // 1. 중복 제거
        HashSet<String> reportSet = new HashSet<String>();
        for(String rep : report){
            reportSet.add(rep);
        }

        // 2. notifyListHash 만들기
        HashMap<String, ArrayList<String>> notifyListHash = new HashMap();
        for(String rep : reportSet) {
            String[] data = rep.split(" ");
            String reporter = data[0];
            String reportee = data[1];

            ArrayList<String> reporterList = notifyListHash.getOrDefault(reportee, new ArrayList());
            reporterList.add(reporter);
            notifyListHash.put(reportee, reporterList);
        }
        // 3. notifyListHash 를 기반으로 reporterHash 만들기
        HashMap<String,Integer> reporterHash = new HashMap();
        for(ArrayList<String> notifyList : notifyListHash.values()) {
            if(notifyList.size() >= k) {
                for(String reporter : notifyList) {
                    reporterHash.put(reporter, reporterHash.getOrDefault(reporter, 0) + 1);
                }
            }
        }

        // 4. reporterHash 를 기반으로 answer 채운다
        for(int i=0; i <id_list.length; i++) {
            answer[i] = reporterHash.getOrDefault(id_list[i], 0);
        }

        return answer;
    }
    
}

참고 : 개발자로 취직하기

 

 

'solve' 카테고리의 다른 글

[프로그래머스/큐] Lv2. 프로세스 (자바)  (0) 2024.01.12
[백준 1764/해시] 실버4/듣보잡 (자바)  (0) 2024.01.11
[프로그래머스/해시] Lv2. 순위 검색 (자바)  (1) 2024.01.11
[프로그래머스/해시] Lv2. 메뉴 리뉴얼 (자바)  (0) 2024.01.11
[프로그래머스/그리디] Lv1. 키패드 누르기(자바)  (1) 2024.01.10
    'solve' 카테고리의 다른 글
    • [프로그래머스/큐] Lv2. 프로세스 (자바)
    • [백준 1764/해시] 실버4/듣보잡 (자바)
    • [프로그래머스/해시] Lv2. 순위 검색 (자바)
    • [프로그래머스/해시] Lv2. 메뉴 리뉴얼 (자바)
    been_dev
    been_dev

    티스토리툴바