https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

일수로 변환해서 풀이하는게 핵심.

 

내 코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

using ll = long long;

struct DT {
    ll totalDays;  // 날짜 또는 보관 가능한 마지막 날
    char termType; // 약관 종류
    int number;    // 개인정보 순번
};

// YYYY.MM.DD 형식의 날짜를 일수로 변환
ll convertToDays(const string& date) {
    int year  = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day   = stoi(date.substr(8, 2));

    return static_cast<ll>(year) * 12 * 28
         + static_cast<ll>(month - 1) * 28
         + day;
}

vector<int> solution(
    string today,
    vector<string> terms,
    vector<string> privacies
) {
    vector<int> answer;

    // 1. 약관별 보관 가능 기간을 일수로 저장
    vector<DT> termData;

    for (const string& term : terms) {
        char termType = term[0];
        int months = stoi(term.substr(2));

        // 수집 당일을 첫째 날로 포함하므로 -1
        ll validDays = static_cast<ll>(months) * 28 - 1;

        termData.push_back({
            validDays,
            termType,
            0
        });
    } // 0은 무시해도됨

    // 2. 개인정보 수집 날짜와 약관 종류 저장
    vector<DT> privacyData;

    for (int i = 0; i < privacies.size(); ++i) {
        const string& privacy = privacies[i];

        ll collectedDays = convertToDays(privacy.substr(0, 10));
        char termType = privacy[11];

        privacyData.push_back({
            collectedDays,
            termType,
            i + 1
        });
    }

    // 3. 각 개인정보의 보관 가능한 마지막 날 계산
    vector<DT> expirationData;

    for (const DT& term : termData) {
        for (const DT& privacy : privacyData) {
            if (term.termType == privacy.termType) {
                expirationData.push_back({
                    privacy.totalDays + term.totalDays,
                    term.termType,
                    privacy.number
                });
            }
        }
    }

    // 4. 오늘이 보관 가능한 마지막 날을 지났는지 검사
    ll todayDays = convertToDays(today);

    for (const DT& privacy : expirationData) {
        if (todayDays > privacy.totalDays) {
            answer.push_back(privacy.number);
        }
    }

    // 약관 순서대로 검사했으므로 개인정보 순번을 다시 정렬
    sort(answer.begin(), answer.end());

    return answer;
}

+ Recent posts