알고리즘 코드카타
select user_id, count(distinct follower_id) as followers_count
from Followers
group by user_id;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
Arrays.fill(answer, 0);
for(String id: id_list){
List<Integer> reportedId = new ArrayList<>();
for (String r : report){
String[] arr = r.split(" ");
if (!arr[1].equals(id)){
continue;
}
int index = finding(id_list, arr[0]);
if (index < 0){
continue;
}
if (reportedId.isEmpty()){
reportedId.add(index);
continue;
}
if (!reportedId.contains(index)){
reportedId.add(index);
}
}
if (reportedId.size() < k){
continue;
}
for (int i: reportedId){
answer[i]++;
}
}
return answer;
}
public int finding(String[] id_list, String nickname){
int index = -1;
for(int i = 0; i < id_list.length; i++){
if(id_list[i].equals(nickname)){
index = i;
break;
}
}
return index;
}
}
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
Map<String, Set<String>> reportList = new HashMap<>();
Map<String, Integer> reportCount = new HashMap<>();
for (String r : report) {
String[] parts = r.split(" ");
reportList.putIfAbsent(parts[0], new HashSet<>());
if (reportList.get(parts[0]).add(parts[1])) {
reportCount.put(parts[1], reportCount.getOrDefault(parts[1], 0) + 1);
}
}
for (int i = 0; i < id_list.length; i++) {
Set<String> part = reportList.getOrDefault(id_list[i], new HashSet<>());
for (String p : part) {
if (reportCount.getOrDefault(p, 0) >= k) {
answer[i]++;
}
}
}
return answer;
}
}