KotlinAlgorithm#15 (POG 개인 정보 수집 유효기간)

박채빈·2023년 7월 12일
0

KotlinAlgorithm

목록 보기
15/28
post-thumbnail

POG 개인 정보 수집 유효기간

링크

코드

import java.time.LocalDate
import java.time.format.DateTimeFormatter


class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        val answer = ArrayList<Int>()

        val todayFormattedDate = LocalDate.parse(today, DateTimeFormatter.ofPattern("yyyy.MM.dd"))
        val termMap = terms.associate { it.split(" ").let { (code, term) ->
            code to term.toInt()
        } }

        privacies.forEachIndexed { index, privacy ->
            privacy.split(" ").let { (startDate, term) ->
                val startFormattedDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy.MM.dd"))
                val expiredFormattedDate = startFormattedDate.plusMonths(termMap[term]!!.toLong())
                if (!expiredFormattedDate.isAfter(todayFormattedDate)) {
                    answer.add(index + 1)
                }
            }
        }
        return answer.toIntArray()
    }
}


class Solution1 {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>) = privacies.indices.filter { privacies[it].split(" ").first().split("\\.".toRegex()).map(String::toInt).let { (y, m, d) -> (y * 12 * 28) + (m * 28) + d } + (terms.map { it.split(" ") }.associate { (a, b) -> a to b.toInt() }.getOrDefault(privacies[it].split(" ").last(), 0) * 28) <= today.split("\\.".toRegex()).map(String::toInt).let { (y, m, d) -> (y * 12 * 28) + (m * 28) + d } }.map { it + 1 }
}

날짜를 LocalDate로 변환해서 비교함.
Solution1 은 좀 변태같아서 가져와봄

profile
안드로이드 개발자

0개의 댓글