Tree's 개발일기
[자바]신고 결과 받기 본문
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. from to를 저장할 map생성(이때, 중복되면 안되기 때문에 값은 hashset을 사용해준다)
*map의 key는 어차피 중복되지 않기 때문에 value값에 set을 사용해주면 된다
2. id_list의 인덱스와 값을 키-값으로 기억해놓고, answer의 index위치에 count를 세려나간다
구현문제인데, 레벨1치고, 설계를 잘해야되서 까다로웠던 문제.
map을 2번 사용해서 index와 중복을 고려해서 풀이할 수 있다.
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
boolean[] sign = new boolean[id_list.length];
Map<String, HashSet<String>> map = new HashMap<>(); //from, to저장
Map<String, Integer> idx_map = new HashMap<>();
for (int i=0; i<id_list.length; i++) {
map.put(id_list[i], new HashSet<>());
idx_map.put(id_list[i], i);
}
for (int i=0; i<report.length; i++) {
String from = report[i].split(" ")[0];
String to = report[i].split(" ")[1];
map.get(to).add(from); //map에서 to값을 가져와서(hashset) from을 추가
}
for (int i=0; i<id_list.length; i++) {
HashSet<String> send = map.get(id_list[i]);
if (send.size() >=k) {
for (String name:send) {
answer[idx_map.get(name)]++;
}
}
}
return answer;
}
}728x90
'문제풀이 > 프로그래머스' 카테고리의 다른 글
| [자바] 달리기경주 (0) | 2023.07.22 |
|---|---|
| 미로 탈출 (0) | 2023.07.20 |
| 삼각달팽이 (0) | 2023.07.16 |
| 햄버거 만들기 (0) | 2023.07.13 |
| 숫자 문자열과 영단어 (0) | 2023.07.11 |
Comments