문제
1 번째 시도
이 문제 자체는 쉬웠지만 자료구조에 대해 고민을 많이 했다.
- Key: 신고당한 사람 Value: 신고한 사람 reportMatching(HashMap)
- Key: 메일 받은 사람 Value: 메일 받은 횟수 lMap(LinkedHashMap)
import java.util.*;
class Solution {
public Collection<Integer> solution(String[] id_list, String[] report, int k) {
ArrayList<ArrayList<String>> reportMatching = new ArrayList<ArrayList<String>>();
HashMap<String, ArrayList<String>> map = new HashMap<>();
for(int i =0; i < id_list.length; i++) {
map.put(id_list[i], new ArrayList<String>());
}
for(int i = 0; i < report.length; i++) {
String[] reporting = report[i].split(" ");
if(!map.get(reporting[1]).contains(reporting[0]))
map.get(reporting[1]).add(reporting[0]);
}
LinkedHashMap<String, Integer> lMap = new LinkedHashMap<String, Integer>();
for(int i =0 ; i < id_list.length; i++) {
lMap.put(id_list[i], 0);
}
for(ArrayList<String> m : map.values()) {
if(m.size() >= k) {
for(String ma : m) {
int currentCount = lMap.get(ma);
lMap.put(ma, currentCount + 1);
}
}
}
return lMap.values();
}
}
몰랐던 거
- HashMap을 순차적으로 탐색하여 value인 ArrayList를 순회할 수 있는지?
- forEach를 사용하여 순회
- map.values()
- map.keySet()
- HashMap Value에 ArrayList를 초기화와 동시에 값을 어떻게 넣는지?
-
reporterInfoMap.put(reporter, new ArrayList<String>(){{ add(reported); }}); }
- 이중 중괄호로 초기화와 동시에 값을 넣을 수 있다.
- 메모리 누수가 발생할 수도 있어 안티패턴으로 취급
- 대신, new ArrayList<>(Arrays.asList(1,2,3))를 사용
- 추가로 말하자면 int[]은 바로 List로 바꿀 수 없습니다. 더 자세할 설명은 이전글을 봐주세요.
-
- 순서가 있는 HashMap
- LinkedHashMap
- Set이 무엇인가?
- Set: 중복처리 자료구조
- Iteraor이 무엇인가?
- Iterator는 컬렉션 프레임워크에 대한 인터페이스
'알고리즘' 카테고리의 다른 글
항해99 20일차 TIL( 전력망을 둘로 나누기 / 프로그래머스) (0) | 2024.04.17 |
---|---|
[프로그래머스] 연속된 부분 수열의 합 / 자바 (1) | 2024.04.16 |
[프로그래머스] 무인도 여행, 자바 (0) | 2024.04.15 |
[프로그래머스] 택배상자, 자바 (0) | 2024.04.15 |
항해 99 18일차 TIL( 대충 만든 자판 / 프로그래머스 ) (0) | 2024.04.15 |