💻 문제 출처 : 프로그래머스_신고 결과 받기
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
// 이름을 Key로, String Set을 Value로 갖는 Map 생성
// => Key의 이름을 신고한 사람 Set을 저장됨
// => Key에 해당하는 Set size가 k 이상이면 해당 Set에 있는 사람들에 대한 answer 배열 요소를 + 1
Map<String, Set<String>> reportMap = new HashMap<>();
for(String s : id_list) {
reportMap.put(s, new HashSet<>());
}
for(String s : report) {
String[] division = s.split(" ");
String reporter = division[0];
String badman = division[1];
reportMap.get(badman).add(reporter);
}
for(String s : id_list) {
if(reportMap.get(s).size() >= k) {
for(String reporter : reportMap.get(s)) {
int index = 0;
for(int i = 0; i < id_list.length; i++) {
if(id_list[i].equals(reporter)) {
index = i;
break;
}
}
answer[index] += 1;
}
}
}
return answer;
}
}
📌 문제 풀이 설명
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
List<String> list = Arrays.stream(report).distinct().collect(Collectors.toList());
HashMap<String, Integer> count = new HashMap<>();
for (String s : list) {
String target = s.split(" ")[1];
count.put(target, count.getOrDefault(target, 0) + 1);
}
return Arrays.stream(id_list).map(_user -> {
final String user = _user;
List<String> reportList = list.stream().filter(s -> s.startsWith(user + " ")).collect(Collectors.toList());
return reportList.stream().filter(s -> count.getOrDefault(s.split(" ")[1], 0) >= k).count();
}).mapToInt(Long::intValue).toArray();
}
}
📌 문제 풀이 설명
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
// key: 신고당한놈, value: 몇명한테 당했는지
Map<String, Set<String>> map = new HashMap<>();
for (String rep : report) {
String[] arr = rep.split(" ");
Set<String> set = map.getOrDefault(arr[1], new HashSet<>());
set.add(arr[0]);
map.put(arr[1], set);
}
// key: 알림받을 놈, value: 몇번 알림받을지
Map<String, Integer> countMap = new LinkedHashMap<>();
for (String id : id_list) {
countMap.put(id, 0);
}
for (Map.Entry<String, Set<String>> entry : map.entrySet()) {
if (entry.getValue().size() >= k) { // 정지당할놈
for (String value : entry.getValue()) {
countMap.put(value, countMap.getOrDefault(value, 0) + 1);
}
}
}
return countMap.values().stream().mapToInt(Integer::intValue).toArray();
}
}
📌 문제 풀이 설명
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
ArrayList<User> users = new ArrayList<>();
HashMap<String,Integer> suspendedList = new HashMap<>(); //<이름>
HashMap<String,Integer> idIdx = new HashMap<String,Integer>(); // <이름, 해당 이름의 User 클래스 idx>
int idx = 0;
for(String name : id_list) {
idIdx.put(name,idx++);
users.add(new User(name));
}
for(String re : report){
String[] str = re.split(" ");
//suspendedCount.put(str[0], suspendedCount.getOrDefault(str[0],0)+1);
users.get( idIdx.get(str[0])).reportList.add(str[1]);
users.get( idIdx.get(str[1])).reportedList.add(str[0]);
}
for(User user : users){
if(user.reportedList.size() >= k)
suspendedList.put(user.name,1);
}
for(User user : users){
for(String nameReport : user.reportList){
if(suspendedList.get(nameReport) != null){
answer[idIdx.get(user.name)]++;
}
}
}
return answer;
}
}
class User{
String name;
HashSet<String> reportList;
HashSet<String> reportedList;
public User(String name){
this.name = name;
reportList = new HashSet<>();
reportedList = new HashSet<>();
}
}
📌 문제 풀이 설명