코딩테스트/프로그래머스
[프로그래머스] 신고 결과 받기 (Java)
개발자마이
2022. 3. 5. 02:19
반응형
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
작성 코드
1. 한 유저당 한 사람에 대해 1회만 신고가 유효하므로 report 중복값을 제거한 후 reportList에 저장한다.
2. reportList를 읽어 신고자와 신고당한자의 이름을 각각 reportArray[i][0], reportArray[i][1]에 저장한다.
3. reportArray를 돌며 각 유저가 신고당한 횟수를 reportCount배열에 저장한다.
4. 정지 당한 유저 (2회 이상)를 체크한 후, 각 신고자가 받을 처리 결과 메일의 수를 체크하여 배열로 저장한다.
//몇몇 케이스 시간초과
import java.util.*;
import java.lang.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = {};
//report 중복값 제거 (한 유저당 1회 신고 가능)
for(int i=0; i<report.length-1; i++){
for(int j=i+1; j<report.length; j++)
if(report[i].equals(report[j])){
report[j] = "";
}
}
List<String> reportList = new ArrayList<String>();
//null값을 제외한 report 내용 리스트에 저장
for(int i=0; i<report.length; i++){
if(report[i]!=""){
reportList.add(report[i]);
}
}
String[][] reportArray = new String[reportList.size()][2];
//report 배열로부터 신고자, 신고당한자 분리하여 배열로 저장
for(int i=0; i<reportArray.length; i++){
StringTokenizer st = new StringTokenizer(reportList.get(i));
reportArray[i][0] = st.nextToken();
reportArray[i][1] = st.nextToken();
}
//신고횟수를 카운트하기위한 배열 선언 (0으로 시작)
int[] reportCount = new int[id_list.length];
Arrays.fill(reportCount, 0);
//신고당한 횟수 카운트하기
for(int i=0; i<reportArray.length; i++){
for(int j=0; j<id_list.length; j++){
if(id_list[j].equals(reportArray[i][1])){
reportCount[j]++;
}
}
}
//정지당한 유저 저장 목록
List<String> suspendedList = new ArrayList<String>();
//카운트가 2가 넘어가는 유저(정지당한 유저)를 신고한 유저들의 id 정보 저장
for(int i=0; i<reportCount.length; i++){
if(reportCount[i] >= k){
for(int j=0; j<reportArray.length; j++){
if(id_list[i].equals(reportArray[j][1])){
suspendedList.add(reportArray[j][0]);
}
}
}
}
//답변 배열 초기화
answer = new int[id_list.length];
Arrays.fill(answer, 0);
for(int i=0; i<suspendedList.size(); i++){
String user = suspendedList.get(i);
for(int j=0; j<id_list.length; j++){
if(user.equals(id_list[j])){
answer[j]++;
}
}
}
return answer;
}
}
논리는 맞는 것 같은데, 몇몇 케이스에서 시간초과가 나서 아직 고민 중에 있음
Note
프로그래머스 연습 문제들 중 카카오 문제들은 레벨1이라도 문제 읽고 푸는 데에 시간이 꽤 걸린다. 효율적인 자료구조와 알고리즘의 적용을 위한 연습이 많이 필요함을 느낀다.
반응형