KO EN

[Kotlin] Korean Coding Test - Programmers - Getting Reported Results

by 민갤

Back End /

Question

Link

Each user can report one user at a time.

  • There is no limit to the number of reports.You can continue to report different users.
  • You can report one user multiple times, but the number of reports for the same user is treated as one time.

Users who have been reported more than k times will be suspended from using the bulletin board, and the suspension will be mailed to all users who reported the user.

  • We collect all the information that the user reported and send a suspension email at the end while suspending the bulletin board at once.

Restrictions

  • The element of id_list is a string that represents the user's id and consists of only lowercase letters.
  • The id_list does not contain the same ID in duplicate.
  • The element of the report is a string with only lowercase letters in the form of "user_id reported_id".
  • User id and reported id are separated by one space.
  • You don't report yourself.
  • The array to be returned can contain the number of mails received by each user in the order of id in the id_list.

Input/Output Example

Input/Output Example #1

  • Input
  • id_list: ["muzi", "frodo", "apeach", "neo"]
  • report: ["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"]
  • k: 2
  • Output [2,1,1,0]

Input/Output Example #2

  • Input
  • id_list: ["con", "ryan"]
  • report: ["ryan con", "ryan con", "ryan con", "ryan con"]
  • k: 3
  • Output : [0,0]

Time limit

Accuracy test: 10 seconds

Code 1

class Solution {
    fun solution(idList: Array<String>, report: Array<String>, k: Int): IntArray {
        // Reported details - cannot be duplicated
        val targetReport = mutableMapOf<String, MutableSet<String>>()
        // If distinct() is used, both List and Set take at least 29 seconds, but if you use Set without distinct, it takes at least 12 seconds
        report.forEach {
            val (reporter, target) = it.split(" ")
            targetReport[target] = targetReport.getOrDefault(target, mutableSetOf())
            targetReport[target]?.add(reporter)
        }

        // Mail history
        val userSendMail = mutableMapOf<String, Int>()
        // Mail the suspension to all users who reported the user
        targetReport
            .filter { it.value.count() >= k }
            .forEach {
                it.value.forEach { userId ->
                    userSendMail[userId] = userSendMail.getOrDefault(userId, 0) + 1
                }
            }
        return idList.map { userSendMail.getOrDefault(it, 0) }.toIntArray()
    }
}
TEST 1 〉	Pass (13.21ms, 61.5MB)
TEST 2 〉	Pass (13.10ms, 62.1MB)
TEST 3 〉	Pass (154.73ms, 143MB)
TEST 4 〉	Pass (12.99ms, 62.2MB)
TEST 5 〉	Pass (14.77ms, 62.3MB)
TEST 6 〉	Pass (23.58ms, 64.5MB)
TEST 7 〉	Pass (20.77ms, 70.6MB)
TEST 8 〉	Pass (44.83ms, 84.6MB)
TEST 9 〉	Pass (126.78ms, 111MB)
TEST 10 〉	Pass (95.07ms, 117MB)
TEST 11 〉	Pass (161.39ms, 150MB)
TEST 12 〉	Pass (16.92ms, 61.9MB)
TEST 13 〉	Pass (18.50ms, 62MB)
TEST 14 〉	Pass (139.77ms, 119MB)
TEST 15 〉	Pass (170.33ms, 148MB)
TEST 16 〉	Pass (14.17ms, 62.9MB)
TEST 17 〉	Pass (22.37ms, 62MB)
TEST 18 〉	Pass (20.16ms, 62.7MB)
TEST 19 〉	Pass (26.16ms, 62.4MB)
TEST 20 〉	Pass (113.04ms, 116MB)
TEST 21 〉	Pass (211.26ms, 141MB)
TEST 22 〉	Pass (12.85ms, 61.2MB)
TEST 23 〉	Pass (12.81ms, 62.1MB)
TEST 24 〉	Pass (13.11ms, 61.2MB)

Code 2

  class Solution {
    fun solution(idList: Array<String>, report: Array<String>, k: Int): IntArray =
        report.distinct() // Remove duplicate declaration
            .map { it.split(" ") }
            .groupBy { it[1] } // Reported details by user
            .filter { it.value.size >= k } // Report history k cases or more
            .map { a -> a.value.map { it[0] } } // Reporter extraction
            .flatten() // Ungroup
            .groupingBy { it }.eachCount()
            .run { idList.map { getOrDefault(it, 0) } }.toIntArray()
}
TEST 1 〉	Pass (35.51ms, 65.4MB)
TEST 2 〉	Pass (28.79ms, 65.3MB)
TEST 3 〉      Pass (31.91ms, 65.8MB)
TEST 5 〉	Pass (44.67ms, 65.6MB)
TEST 6 〉	Pass (40.54ms, 65.4MB)
TEST 7 〉	Pass (39.13ms, 72.6MB)
TEST 8 〉	Pass (39.47ms, 83.5MB)
TEST 9 〉	Pass (150.46ms, 131MB)
TEST 10 〉	Pass (101.38ms, 113MB)
TEST 11 〉	Pass (234.00ms, 179MB)
TEST 12 〉	Pass (35.69ms, 67MB)
TEST 13 〉	Pass (41.59ms, 65.5MB)
TEST 14 〉	Pass (139.25ms, 119MB)
TEST 15 〉	Pass (130.72ms, 130MB)
TEST 16 〉	Pass (30.91ms, 66.7MB)
TEST 17 〉	Pass (34.17ms, 66MB)
TEST 18 〉	Pass (40.42ms, 66MB)
TEST 19 〉	Pass (49.65ms, 66.5MB)
TEST 20 〉	Pass (124.52ms, 116MB)
TEST 21 〉	Pass (175.12ms, 128MB)
TEST 22 〉	Pass (29.46ms, 64.8MB)
TEST 23 〉	Pass (30.37ms, 65.5MB)
TEST 24 〉	Pass (30.01ms, 65.4MB)

Author

민갤

민갤

Back-End Developer

꾸잉꾸잉하고 웁니다.

로그인

디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓