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

김세희·2025년 6월 26일

✍️Today I Learned

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


문제 설명

프로그래머스 문제 링크
today에 유효기간이 끝난 개인정보가 있는지 확인하여 파기해야 하는 개인정보의 번호 목록 반환해라
terms: 약관 별 유효기간 목록
privacies: 개인정보 수집일자별 약관 종류
제한사항

  • today는 YYYY.MM.DD 형식이다.
  • 1 ≤ term 길이 ≤ 20
    • term의 원소는 "약관 종류 유효기간"의 형태이다.
    • 약관 종류는 알파벳 대문자 하나이며 term에서 중복되지 않는다.
    • 유효기간은 달 수를 나타내는 정수이며 1 이상 100 이하 이다.
  • 1 ≤ privacies 길이 ≤ 100
    • privacies[i]i+1번의 개인정보 수집 일자와 약관 종류를 나타낸다.
    • privacies의 원소를 "날짜 약관 종류의 형태이다.
  • 모든 달은 28일까지 있다고 가정한다.

풀이

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

using namespace std;
// 모든 달은 28일까지 
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    unordered_map<string, int> mapTerms;
    // today의 총 일수
    int t_totalDays = stoi(today.substr(0, 4))*12*28 
        + (stoi(today.substr(5, 2))-1)*28 + stoi(today.substr(8, 2)) - 1;
    // terms map에 넣기
    for (auto s : terms)
    {
        mapTerms[s.substr(0, 1)] = stoi(s.substr(2));
    }

    for (int i = 0; i < privacies.size(); i++)
    {
        string term = privacies[i].substr(11);
        int YYYY = stoi(privacies[i].substr(0, 4));
        int MM = stoi(privacies[i].substr(5, 2));
        int DD = stoi(privacies[i].substr(8, 2));
        
        int term_totalDays = YYYY*12*28 + (MM+mapTerms[term]-1)*28 + DD - 1 - 1;
        
        if (t_totalDays > term_totalDays)
        {
            answer.push_back(i + 1);
        }
    }
    return answer;
}

코드 설명

  1. unordered_map 타입의 변수에 terms약관 종류를 key, 유효기간을 value로 추가했다.
  2. today의 총 일수와 privacies 원소 중 날짜 +약관별 유효기간 -1일의 총 일수를 비교하여 유효기간이 만료됐는지 확인했다.

0개의 댓글