[Kotlin] Korean Coding Test - Programmers - Personal Information Collection Expiration Date

Kotlin

Language :

Question

Link

There are n pieces of personal information that are classified as numbers 1 to n collected with the consent of the customer.There are many types of terms and conditions, and each of them has a set expiration date for storing personal information.You know what terms and conditions each personal information was collected.The collected personal information can only be stored before the expiration date, and if the expiration date has passed, it must be destroyed.

For example, if the term A is valid for 12 months and the personal information collected on January 5, 2021 was collected under the terms A, the personal information can be kept until January 4, 2022 and must be destroyed from January 5, 2022.You are trying to get the personal information numbers that need to be destroyed as of today.

All months are assumed to be up to the 28th.

The parameters include today's date, one-dimensional string array terms containing the expiration date of the terms and conditions, and one-dimensional string array privacy containing information on the collected personal information.At this time, complete the solution function so that the number of personal information to be destroyed is returned in ascending order in a one-dimensional integer array.

Restrictions

today

  • Indicates today's date in the form "YYYYY.MM.DD".

terms

  • Each element is a string separated by a single space between the type and expiration date of the terms and conditions in the form of "Term Type Expiration Date".
  • Type of Terms: One uppercase letter in A-Z.Terms and conditions are not duplicated in the terms array.
  • Validity period: An integer representing the number of months that personal information can be stored (1 <= Validity period <= 100)

privacies

  • Each element is a single space-separated string of dates and terms and conditions in the form of "Date Terms and Conditions Type".
  • The date represents the date on which personal information in the form "YYYYY.MM.DD" was collected, and only dates prior to today are given.
  • The privacies terms and conditions are always given only the terms and conditions shown in terms.
  • Privacy[i] indicates the date of collection of personal information i+1 and the type of terms and conditions.

Date "YYYYY.MM.DD"

  • 2000 ≤ YYYY ≤ 2022
  • 1 ≤ MM ≤ 12. For single digits, 0 is preceded.
  • 1 ≤ DD ≤ 28. For single digits, 0 is preceded.

You will only be given an input that has at least one personal information that needs to be destroyed.

Input/Output Example

Input/Output Example #1

  • Input
  • today: "2022.05.19"
  • terms: ["A 6", "B 12", "C 3"]
  • privacies: ["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"]
  • Output: [1, 3]

Input/Output Example #2

  • Input
  • today: "2020.01.01"
  • terms: ["Z 3", "D 5"]
  • privacies: ["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"]
  • Output: [1, 4, 5]

Code

Since all months are assumed to have up to 28 days, the number of days is calculated and compared directly.

class Solution {

    companion object {
        private const val DAYS_OF_MONTH = 28
    }

    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        val todayOfDays = convertDateOfNumberOfDays(today)

        val termMap = hashMapOf<String, Int>()
        terms.forEach {
            val tmp = it.split(" ")
            termMap[tmp[0]] = tmp[1].toInt()
        }

        var answer = intArrayOf()
        for (i in privacies.indices) {
            val tmp = privacies[i].split(" ")
            val validityPeriod = convertDateOfNumberOfDays(tmp[0]) + termMap[tmp[1]]!! * DAYS_OF_MONTH
            
            if (todayOfDays > validityPeriod) answer += i + 1
        }
        return answer
    }

    private fun convertDateOfNumberOfDays(day: String): Int {
        val tmp = day.split(".")
        return tmp[2].toInt() + (tmp[1].toInt() - 1) * DAYS_OF_MONTH + tmp[0].toInt() * 12 * DAYS_OF_MONTH
    }
}
TEST 1 〉	Pass (16.51ms, 63.8MB)
TEST 2 〉	Pass (18.48ms, 63.8MB)
TEST 3 〉	Pass (16.08ms, 64.4MB)
TEST 4 〉	Pass (15.41ms, 64.1MB)
TEST 5 〉	Pass (28.64ms, 63MB)
TEST 6 〉	Pass (24.48ms, 63.2MB)
TEST 7 〉	Pass (28.17ms, 63MB)
TEST 8 〉	Pass (26.87ms, 64.5MB)
TEST 9 〉	Pass (22.01ms, 63.6MB)
TEST 10 〉	Pass (18.40ms, 63.4MB)
TEST 11 〉	Pass (16.54ms, 64.2MB)
TEST 12 〉	Pass (24.17ms, 63.5MB)
TEST 13 〉	Pass (23.84ms, 63.5MB)
TEST 14 〉	Pass (18.27ms, 63.7MB)
TEST 15 〉	Pass (24.19ms, 64MB)
TEST 16 〉	Pass (21.19ms, 64.7MB)
TEST 17 〉	Pass (16.92ms, 63.9MB)
TEST 18 〉	Pass (17.29ms, 64.6MB)
TEST 19 〉	Pass (17.57ms, 64.2MB)
TEST 20 〉	Pass (16.94ms, 64.1MB)

민갤

Back-End Developer

백엔드 개발자입니다.