Programmers Algorithm - 개인정보 수집 유효기간

Myung Jin Kim·2023년 9월 17일
0

이번 문제는 개인 정보 수집에 따른 만료일을 계산하고 폐기해야 하는 개인 정보들을 구하는 문제다.

단순히 날짜를 계산하는거라면, Date Object 를 통해서 만료일을 계산하고 이를 통해 폐기 여부를 구할 수 있지만 모든 달은 28일까지 존재한다는 조건 때문에 쓸 수 없어서 직접 계산을 해야 한다.

결국, 이번 문제는 날짜 계산을 하는 것이고 이에 대해 필요한 예외처리가 핵심이다.

const MAX_DAYS = 28;
const MAX_MONTHS = 12;

function solution(today, terms, privacies) {
    const termMap = {};
    terms.forEach(eachTerm => {
        const [term, months] = eachTerm.split(' ');
        termMap[term] = months;
    });
    
    const answer = [];
    privacies.forEach((eachPrivacy, index) => {
        const [startDate, term] = eachPrivacy.split(' ');
        const [startYear, startMonth, startDay] = startDate.split('.');
        const final = {
            year: Number(startYear),
            month: Number(startMonth) + Number(termMap[term]),
            day: Number(startDay) - 1,  
        };

        const addYear = Math.floor(final.month / MAX_MONTHS);
        const calcMonth = final.month % MAX_MONTHS;
        if(calcMonth === 0) {
            final.year += (addYear - 1);            
            final.month = MAX_MONTHS;
        } else {
            final.month = calcMonth;
            final.year += addYear;            
        }

        
        
        if(final.day === 0) {
            final.month -= 1;
            final.day = MAX_DAYS;
        }
        
        const formattedMonth = final.month.toString().padStart(2, '0');
        const formattedDay = final.day.toString().padStart(2, '0');
        const finalDate = `${final.year}.${formattedMonth}.${formattedDay}`;
   
        if(finalDate < today) {
            answer.push(index + 1);
        }
    });
    return answer;
}

초과여부는 문자열 비교만으로 가능하기 때문에 쉽게 구현할 수 있다.

profile
개발을 취미로 하고 싶은 개발자를 목표로 살고 있습니다

0개의 댓글