GitHubSeob

C++ / 프로그래머스 / 개인정보 수집 유효기간 본문

Programmers/Level 1

C++ / 프로그래머스 / 개인정보 수집 유효기간

GitHubSeob 2023. 6. 21.

문제

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

 

프로그래머스

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

programmers.co.kr

문제풀이

map을 이용해 약관의 종류인 알파벳을 넣으면 유효기간인 달이 나오도록 한다.

년, 월, 일을 일 수로 변환해서 서로 비교를 한다.

년은 년*12*28을 하면 일수로, 월은 월*12을 하면 일수로 변환된다.

일을 구할 때 년 일수+ 월 일수+ 일수+ 약관 일수를 모두 더한다. (약관은 달이므로 12를 곱한다)

일수로 변환한 오늘이 더 크다면 유효기간이 지났으므로 answer에 저장한다.

 

코드

#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    int idx(0), year(0), month(0), day(0), sum(0), i_today(0);
    char alp(' ');
    vector<int> answer;
    unordered_map<char, int>period;

    for (idx = 0; idx < terms.size(); ++idx) {
        period[terms[idx][0]] = stoi(terms[idx].substr(2, terms[idx].size() - 1));
    }

    i_today = stoi(today.substr(0, 4)) * 12 * 28 
        + stoi(today.substr(5, 6)) * 28 
        + stoi(today.substr(8, 9));

    for (idx = 0; idx < privacies.size(); ++idx) {
        year = stoi(privacies[idx].substr(0, 4));
        month = stoi(privacies[idx].substr(5, 6));
        day = stoi(privacies[idx].substr(8, 9));

        alp = privacies[idx][11];

        sum = year * 12 * 28 
            + month * 28 
            + day - 1 
            + period[alp] * 28;

        if (i_today > sum) {
            answer.push_back(idx + 1);
        }
    }

    return answer;
}