https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. 문제 설명
- 변수
id_list : 이용자의 아이디
report : "신고한 아이디 신고당한 아이디"
k : 정지 기준 횟수
- 결과
- 정지 기준에 도달하면 신고한 아이디에 정지결과메일 +1
- 메일 받은 횟수를 배열로 제출
- 주의사항
한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리
2. 풀이과정
- 신고한 아이디별로 신고당한 아이디를 정리(SET으로 중복 삭제)
- putIfAbsent
- Key값이 존재하는 경우 value를 반환
- Key값이 존재하지 않는 경우 Key와 Value를 Map에 저장하고 Null 반환
# 신고한 아이디별로 신고당한 아이디를 정리(SET으로 중복 삭제)
HashMap<String, HashSet<String>> reportSetMap = new HashMap<>();
for (String r : report) {
String[] reportInfo = r.split(" ");
reportSetMap.putIfAbsent(reportInfo[1], new HashSet<>());
reportSetMap.get(reportInfo[1]).add(reportInfo[0]);
}
- k번 이상 신고당한 유저 확인 후, 유저별로 map에 정지결과 메일횟수 추가
HashMap<String, Integer> map = new HashMap<>();
for (String key : reportSetMap.keySet()) {
// k번 이상 신고된 유저
if (reportSetMap.get(key).size() >= k) {
// 유저별로 신고한 유저에게 정지결과 메일횟수 횟수 추가
for (String reporter : reportSetMap.get(key)) map.put(reporter, map.getOrDefault(reporter, 0) + 1);
}
}
- 전체 코드
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
// 신고한 아이디, 신고당한 아이디 정리(SET으로 중복제거)
HashMap<String, HashSet<String>> reportSetMap = new HashMap<>();
for (String r : report) {
String[] reportInfo = r.split(" ");
reportSetMap.putIfAbsent(reportInfo[1], new HashSet<>());
reportSetMap.get(reportInfo[1]).add(reportInfo[0]);
}
// 아이디별 메일 갯수를 넣을 map
HashMap<String, Integer> map = new HashMap<>();
for (String key : reportSetMap.keySet()) {
// k번 이상 신고된 유저
if (reportSetMap.get(key).size() >= k) {
// 유저별로 신고한 유저에게 정지결과메일 횟수 추가
for (String reporter : reportSetMap.get(key)) map.put(reporter, map.getOrDefault(reporter, 0) + 1);
}
}
// 아이디 별로 메일수 담기
for (int i = 0; i < id_list.length; i++) {
answer[i] = map.getOrDefault(id_list[i], 0);
}
return answer;
}
}
- 다른 사람 코드
객체지향을 활용하고자한 점에서 배울점이 많아 보여 가져왔습니다.
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<>();
}
}'코딩 알고리즘 스터디' 카테고리의 다른 글
| 프로그래머스 Lv.1 대충 만든 자판도움말 - Java (1) | 2025.01.12 |
|---|---|
| 프로그래머스 Lv.1 [2024 KAKAO WINTER INTERNSHIP] 가장 많이 받은 선물 - JAVA (0) | 2025.01.05 |
| 프로그래머스 Lv.2 메뉴 리뉴얼 (Java) (2) | 2024.12.16 |
| 코딩마스터스 (중급) 메타버스 토끼 (2) | 2024.12.10 |
| 프로그래머스 중급 블로그 (0) | 2024.12.04 |