HashMap은 Key로 찾는 database 같다. 그리고 Array나 ArrayList와 달리 Key로 찾으니 시간효율성이 매우 좋음
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = {};
HashMap <String, ArrayList<String>> singoWho = new HashMap <String, ArrayList<String>>();
HashMap <String, Integer> singoGetCount = new HashMap <String, Integer>();
HashMap <String, Integer> mailGetCount = new HashMap <String, Integer>();
HashMap <String, Integer> IsItNew = new HashMap <String, Integer>();
// 신고받은 횟수, 메일받은 횟수 캐릭터별 0으로 초기화
for (int i = 0; i < id_list.length; i++){
singoGetCount.put(id_list[i], 0);
mailGetCount.put(id_list[i], 0);
}
// report에 뒷 캐릭터의 신고받은 횟수 +1
for (int i = 0; i < report.length; i++){
if (IsItNew.get(report[i]) == null){
String[] a = report[i].split(" ");
int count = singoGetCount.get(a[1]);
count++;
singoGetCount.put(a[1], count);
// System.out.println(a[1] + " : " + count);
}
IsItNew.put(report[i], 1);
}
// report에 뒷 캐릭터의 신고받은 횟수가 k보다 크면, 앞 캐릭터의 메일받은 횟수 +1
for (int i = 0; i < report.length; i++){
String[] a = report[i].split(" ");
if(singoGetCount.get(a[1]) >= k){
int count = mailGetCount.get(a[0]);
count++;
mailGetCount.put(a[0], count);
}
}
//id_list 순서대로 answer에 메일받은 횟수 넣기
answer = new int[id_list.length];
for (int i = 0; i < id_list.length; i++){
answer[i] = mailGetCount.get(id_list[i]);
}
return answer;
}
}
결과는 처참...
-> 아예 답이 틀리게 나왔다. 알고보니 메일받은 횟수를 세는 for 문에서 오류가 있었던 것(중복 신고 여부를 확인하지 않음)
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = {};
HashMap <String, Integer> singoGetCount = new HashMap <String, Integer>();
HashMap <String, Integer> mailGetCount = new HashMap <String, Integer>();
HashMap <String, Integer> IsItNew = new HashMap <String, Integer>();
// 신고받은 횟수, 메일받은 횟수 캐릭터별 0으로 초기화
for (int i = 0; i < id_list.length; i++){
singoGetCount.put(id_list[i], 0);
mailGetCount.put(id_list[i], 0);
}
// report에 뒷 캐릭터의 신고받은 횟수 +1
for (int i = 0; i < report.length; i++){
if (IsItNew.get(report[i]) == null){
String[] a = report[i].split(" ");
int count = singoGetCount.get(a[1]);
count++;
singoGetCount.put(a[1], count);
// System.out.println(a[1] + " : " + count);
}
IsItNew.put(report[i], 1);
}
IsItNew.clear();
// report에 뒷 캐릭터의 신고받은 횟수가 k보다 크면, 앞 캐릭터의 메일받은 횟수 +1
for (int i = 0; i < report.length; i++){
if (IsItNew.get(report[i]) == null){
String[] a = report[i].split(" ");
if(singoGetCount.get(a[1]) >= k){
int count = mailGetCount.get(a[0]);
count++;
mailGetCount.put(a[0], count);
}
}
IsItNew.put(report[i], 1);
}
//id_list 순서대로 answer에 메일받은 횟수 넣기
answer = new int[id_list.length];
for (int i = 0; i < id_list.length; i++){
answer[i] = mailGetCount.get(id_list[i]);
}
return answer;
}
}
메일 받은 횟수를 세는 for문에서도 중복 신고 여부를 확인했더니 깔끔하게 성공~ 실력이 늘어가는게 보인닷!
이제 레벨2로 점푸할 시간