알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄

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

 

프로그래머스

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

programmers.co.kr


Think🤔
	public static void main(String[] args) {
		//String[] id_list, String[] report, int k
		String[] id_list = {"muzi", "frodo", "apeach", "neo"};
		String[] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
		int k = 2;
		
		int[] report_list = new int[id_list.length];
		String[] report_back = new String[2]; // 신고받은 사람 
		
		for(int i=0; i<report.length; i++) {
			report_back = report[i].split(" ");
			for(int j=0; j<id_list.length; j++) {
				if(report_back[1].equals(id_list[j])) {
					report_list[j]++;
				}
			}
		}
		
		ArrayList arr = new ArrayList<>(); // 배열 크기가 얼마나 될지 모르므로 ArrayList 선언
		for(int i=0; i<report_list.length; i++) {
			if(report_list[i] >= k) {
				arr.add(id_list[i]);
			}
		}
		
		HashMap<String,Integer> map = new HashMap<>();
		for(int i=0; i<report.length; i++) {
			report_back = report[i].split(" ");
			for(int j=0; j<arr.size(); j++) {
				if(report_back[1].equals(arr.get(j))) {
					map.put(report_back[0], map.getOrDefault(report_back[0], 0)+1);
				}
			}
		}
		
		int[] ret = new int[id_list.length];
		for(int i=0; i<id_list.length; i++) {
			ret[i] = (map.get(id_list[i]) == null ? 0 :map.get(id_list[i]));
		}
		
		System.out.println(Arrays.toString(ret));
	}

신고가 k이상인 사람들을 찾아서 그 앞에 있는 ex) "무지 프로도" 이면 프로도가 신고 당했으니

무지의 위치를 찾아서 ++해주는 방법

두 번째 조건인 한 사람이 여러번 신고했을때를 걸러주지 못함..


Solution✍
package kcue.test;

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

import org.aspectj.weaver.ast.Instanceof;

public class reportResult {

	public static void main(String[] args) {
		//String[] id_list, String[] report, int k
		String[] id_list = {"muzi", "frodo", "apeach", "neo"};
		String[] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
		int k = 2;
		
		int[] retMail = new int[id_list.length]; // 받을 메일 횟수
		HashMap<String,Integer> reportMap = new HashMap<>();
		HashMap<String,HashSet<String>> reportMapNum = new HashMap<>();
		
		// 맵 세팅
		for(int i=0; i<id_list.length; i++) {
			reportMap.put(id_list[i], i);
			reportMapNum.put(id_list[i], new HashSet<String>());
		}
		
		// 보내는 사람 se 받는 사람 re
		for(int i=0; i<report.length; i++) {
			String[] split = report[i].split(" "); // 띄어쓰기로 구분
			String se = split[0];
			String re = split[1];
			reportMapNum.get(re).add(se);
		}
	
		for(int i=0; i<id_list.length; i++) {
			 HashSet<String> set = reportMapNum.get(id_list[i]);
			 if(set.size() >= k) {
				 for(String userid : set) {
					 retMail[reportMap.get(userid)]++;					 
				 }
			 }
		}
		
		System.out.println(Arrays.toString(retMail));
		
	}

}

HashSet을 이용해서 중복을 방지하고 , 

해쉬맵을 두개를 이용해서 

1. 이용자의 위치

2. 보내는 사람 , 받는 사람(중복 X)

 

를 이용해서 신고를 K이상 했을 경우 안에서 성공한 유저들에게 ++를 해준다,


Review🤩

내부에서 get.add 가능!


 

+ Recent posts