알고리즘/programmers

[Programmers] 개인정보 수집 유효기간

꾸준함. 2023. 1. 7. 07:28

문제 링크입니다: https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

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

programmers.co.kr

문자열 파싱만 잘하면 쉽게 풀 수 있는 문제였습니다.

 

#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
typedef struct
{
int year;
int month;
int day;
string term;
} Privacy;
map<string, int> term2validity;
Privacy convertToPrivacy(string date)
{
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(5, 2));
int day = stoi(date.substr(8, 2));
string term = date.length() >= 11 ? date.substr(11) : "";
return { year, month, day, term };
}
bool isOutdated(string today, string cur)
{
Privacy todayPrivacy = convertToPrivacy(today);
Privacy curPrivacy = convertToPrivacy(cur);
int validMonth = term2validity[curPrivacy.term];
int day = curPrivacy.day;
int month = curPrivacy.month + validMonth;
int year = curPrivacy.year;
while (month > 12)
{
year++;
month -= 12;
}
if (year > todayPrivacy.year)
{
return false;
}
if (year < todayPrivacy.year)
{
return true;
}
if (month > todayPrivacy.month)
{
return false;
}
if (month < todayPrivacy.month)
{
return true;
}
if (day > todayPrivacy.day)
{
return false;
}
return true;
}
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> answer;
for (string term : terms)
{
term2validity[term.substr(0, 1)] = stoi(term.substr(2));
}
int idx = 1;
for (string privacy : privacies)
{
if (isOutdated(today, privacy))
{
answer.push_back(idx);
}
idx++;
}
return answer;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경: Programmers IDE

지적, 조언, 질문 환영합니다! 질문 남겨주세요~

반응형