[ 다먹살 ]/- Coding

[프로그래머스] 레벨1 신고 결과 받기

엉망으로살기 2022. 4. 2. 19:00
반응형

https://programmers.co.kr/learn/courses/30/lessons/92334?language=java 

 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

 

이 문제는 프로그래머스에 레벨1 중 가장 최신 문제이다. 여기서 주의해야 할 것은 같은 사람이 여러 번 신고했을 경우에 중복처리가 아닌 1회로 처리를 해야한다는 점이다. 아래와 같은 순서를 통해 문제를 해결할 수 있었다.

 

1. HashSet을 이용해 중복된 신고ID - 신고대상ID에 대한 중복값 제거 처리

2. HashMap을 이용해 report로 입력받은 값들에 대한 신고대상ID를 key값으로 하고, value는 ArrayList를 이용해 신고ID를 그때그때 추가

3. HashMap에서 저장해놓은 ID를 비교해서 k번보다 많은 신고가 들어온 대상ID일 경우, 신고ID를 id_list의 ID와비교해서 +한 후 결과값 answer 출력 


문제


제한사항 및 입출력

 


코드

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

class Solution
{
    public static int[] solution(String[] id_list, String[] report, int k)
    {
        // 1. 입력값 처리 및 변수 초기화
        HashSet<String> set = new HashSet<String>();
        HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
        int[] answer = new int[id_list.length];
        String singo = "";
        String daesang = "";
    
        // 2. 중복값 제거
        for(int i=0; i<report.length; i++)
        {
         set.add(report[i]);
        }
        
        // 3. 신고대상 ID별 신고한 ID 처리
        String[] input = (String[]) set.toArray(new String[0]);
        
        for(int i=0; i<input.length; i++)
        {
         String[] temp = input[i].split(" ");
         singo = temp[1];
         daesang = temp[0];
         ArrayList<String> newValue = new ArrayList<String>();
        
         if(map.containsKey(singo))
         {
         newValue = map.get(singo);
         }
        
         newValue.add(daesang);
         map.put(singo, newValue);
        }
        
        Object[] result = map.entrySet().toArray();
        
        // 4. id_list와 신고한 ID를 비교해서 결과값 출력
        for(int i=0; i<result.length; i++)
        {
         String singoPerson = result[i].toString().split("=")[0];
         String[] people = result[i].toString().split("=")[1].replace("[","").replace("]","").split(", ");

         if(people.length>=k)
         {
            for(int j=0; j<people.length; j++)
            {
               for(int m=0; m<id_list.length; m++)
               {
                  if(people[j].equals(id_list[m]))
                  {
                     answer[m]++;
                  }
               }
            }
         }
      }
        
     return answer;
   }
}

반응형