HashMap<String, MytableSet<String>> map 을 정의한다. key는 신고 받은 유저, value는 key를 신고한 유저 리스트를 중복없이 저장한다.
report 의 "from to" 아이템들을 map 에 저장한다.
id_list 를 for문 돌면서, id가 key를 신고를 한 유저이면서 key를 신고한 전체 유저의 수가 k 이상인 key의 개수를 answer 에 추가한다.
def solution(id_list, report, k):
answer = [0] * len(id_list)
dic = {x: [] for x in id_list}
# a가 b 를 신고했다.
for r in set(report):
a = r.split()[0]
b = r.split()[1]
dic[b].append(a)
for d in dic:
if len(dic[d]) >= k:
for a in dic[d]:
answer[id_list.index(a)] += 1
return answer
fun solution(id_list: Array<String>, report: Array<String>, k: Int): IntArray {
val answer = arrayListOf<Int>()
val map: HashMap<String, MutableSet<String>> = hashMapOf()
report.map {
it.split(" ")
}.forEach { item ->
val from = item[0]
val to = item[1]
if (map.contains(to)) {
map[to]?.add(from)
} else {
map[to] = mutableSetOf(from)
}
}
id_list.forEach { id ->
answer.add(
map.filter {
it.value.contains(id) && it.value.size >= k
}.count()
)
}
return answer.toIntArray()
}
list.index(item)flatten()val list = listOf(
listOf(1),
listOf(2, 3),
listOf(1, 2, 3)
)
println(list.flatten()) // [1, 2, 3, 1, 2, 3]
println(list.flatten().distinct()) // [1, 2, 3]