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

JUNWOO KIM·2024년 1월 19일
0

알고리즘 풀이

목록 보기
77/105

프로그래머스 개인정보 수집 유효기간 문제 풀이를 진행하였습니다.

문제 해석

문제를 읽으면 아래와 같은 해석이 가능합니다.

고객의 약관 동의를 얻어서 수집된 1~n번으로 분류되는 개인정보 n개가 있으며 각 개인정보는 저장 유효기간이 정해져있습니다.
각 가인정보는 어떠한 약관으로 수집되었는지 기록되어 있으며 약관에 따라 저장 유효기간이 달라집니다.
현재 날짜를 기점으로 여러 개인정보들과 약관을 확인하여 개인정보가 파기되어야하는지 구해야합니다.
정답은 오름차순으로 담아서 저장해야 합니다.

문제 풀이

string으로 적혀있는 약관과 그에 맞는 유효기간은 <string, int>식으로 unordered_map을 사용하여 쉽게 접근이 가능하도록 저장해줍니다.
그리고 현재 날짜보다 개인정보의 날짜 + 유효기간의 달을 더한 값이 더 전에 있을 경우 그 개인정보는 파기되어야 합니다.
이러한 부분을 각각 년,월,일로 나누어 비교해주며 해당하는 개인정보의 번호를 저장한 후 return해주면 됩니다.

전체 코드

#include <bits/stdc++.h>
#include <string>
#include <vector>

using namespace std;

bool checkDay(string date, string personalDate, int addMonth){
    int index = 0;
    vector<int> curDate;
    vector<int> persDate;
    string str1 = "";
    string str2 = "";
    //년, 월, 일 따로 나눠줌
    while(index < date.size())
    {
        if(date[index] != '.'){
            str1 = str1 + date[index];
            str2 = str2 + personalDate[index];
        }else{
            curDate.push_back(stoi(str1));
            persDate.push_back(stoi(str2));
            str1 = "";
            str2 = "";
        }
        index++;
    }
    curDate.push_back(stoi(str1));
    persDate.push_back(stoi(str2));
    persDate[1] += addMonth;
    
    while(persDate[1] > 12)
    {
        persDate[0]++;
        persDate[1] -= 12;
    }
 
    //만약 현재날짜보다 더 전의 날짜를 가리키면 true
    if(curDate[0] > persDate[0] ||
       curDate[0] == persDate[0] && curDate[1] > persDate[1] ||
       curDate[0] == persDate[0] && curDate[1] == persDate[1] && curDate[2] >= persDate[2])
        return true;
    
    return false;
}

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    unordered_map<string, int> term;
    //약관에 따른 저장하는 유효기간의 값 저장
    for(string str : terms)
    {
        int num = stoi(str.substr(2, str.size() - 2));
        term.emplace(make_pair(str.substr(0, 1), num));
    }
    //앞에서부터 만료됬는지 확인
    for(int i = 0; i < privacies.size(); i++)
    {
        int month = term[privacies[i].substr(11, 1)];
        if(checkDay(today, privacies[i], month))
        {
            answer.push_back(i+1);
        }
    }
    
    return answer;
}

출저

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

profile
게임 프로그래머 준비생

0개의 댓글