관리 메뉴

LC Studio

Programmers Lv1 개인정보 수집 유효기간 (Kotlin) 본문

Java/Programmers

Programmers Lv1 개인정보 수집 유효기간 (Kotlin)

Leopard Cat 2023. 8. 9. 22:56

코딩테스트 연습 - 개인정보 수집 유효기간 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 


문제

개인정보 수집 유효기간을 비교하여, 파기되어야 할 개인정보를 분류하는 문제이다.

 

풀이

현재날짜와 (개인정보 수집일자 + 약관 종류별 일자)를 비교하면 된다. 

자세한 풀이는 아래 주석을 참고하자.

class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        var answer = mutableListOf<Int>()
        val todayTotal = totalDay(today)
        val map = hashMapOf<String, Int>() 
        
        //terms정보를 term변수에 hashMap 형태로 저장
        for(i in terms.indices) { 
            val token = terms[i].split(" ")
            map[token[0]] = token[1].toInt()
        }
        
        //privacies정보를 privacie변수에 hashMap 형태로 저장
        for(i in privacies.indices) {
            val token = privacies[i].split(" ")
            
            val date = token[0]
            val type = token[1]
            val dateTotal = totalDay(date)
            val month = map[type] ?: 0
            val changeDay = month * 28
            
            //오늘날짜가 수집일자+약관종류의 유효기간 보다 크다면 정답에 추가
            if(todayTotal >= (dateTotal + changeDay)){
                answer.add(i+1)
            }
        }
        
        return answer.toIntArray()
    }
    
    //날짜를 일자로 변환
    private fun totalDay(todayTotal: String): Int {

        var sum = 0
        val token = todayTotal.split(".")
        val y = token[0]
        val m = token[1]
        val d = token[2]

        //문제의 조건에 맞게 일자로 변환
        sum += y.toInt() * 12 * 28 
        sum += (m.toInt() - 1) * 28
        sum += d.toInt()

        return sum
	}
}

 

반응형