개인정보 수집 유효기간
프로그래머스 level 1 구현
문제 링크
문제 정보
제한사항
문제 풀이
주어진 사항들을 차례대로 구현 해 풀이했다.
오늘의 날짜를 년, 월, 일로 각각 쪼개어 저장해주고, 약관의 개월수를 hash map 을 이용해 저장해줬다.
회원이 가입한 날짜를 기준으로 약정에 있는 개월 수 만큼 더한 기간부터 폐기 가능 일자라서 거기에서 하루를 빼, 후에 확인하기 편하게 해줬다
이런식으로 접근하면 현재 날짜를 기준으로 이전이기만 하면 무조건 폐기 대상이다.
코드
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
struct info {
int num;
int year;
int month;
int day;
};
unordered_map<char, int> option;
vector<int> parse_date(const string& date) {
vector<int> date_info(3);
date_info[0] = stoi(date.substr(0, 4));
date_info[1] = stoi(date.substr(5, 2));
date_info[2] = stoi(date.substr(8, 2));
return date_info;
}
void cal_custom_info(vector<string>& privacies, vector<info>& custom_info) {
custom_info.reserve(privacies.size());
for (int i = 0; i < privacies.size(); i++) {
info tmp_info;
tmp_info.num = i + 1;
tmp_info.year = stoi(privacies[i].substr(0, 4));
tmp_info.month = stoi(privacies[i].substr(5, 2)) + option[privacies[i].back()];
tmp_info.day = stoi(privacies[i].substr(8, 2)) - 1;
if (tmp_info.day == 0) {
tmp_info.month--;
if (tmp_info.month == 0) {
tmp_info.month = 12;
tmp_info.year--;
}
tmp_info.day = 28;
}
while (tmp_info.month > 12) {
tmp_info.year++;
tmp_info.month -= 12;
}
custom_info.push_back(tmp_info);
}
}
vector<int> ck_terms(const vector<int>& todays_info, const vector<info>& custom_info) {
vector<int> res;
res.reserve(custom_info.size());
for (const auto& cus : custom_info) {
if (cus.year < todays_info[0] ||
(cus.year == todays_info[0] && cus.month < todays_info[1]) ||
(cus.year == todays_info[0] && cus.month == todays_info[1] && cus.day < todays_info[2])) {
res.push_back(cus.num);
}
}
return res;
}
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
vector<int> todays_info = parse_date(today);
vector<info> custom_info;
for (const auto& term : terms) {
option[term[0]] = stoi(term.substr(2));
}
cal_custom_info(privacies, custom_info);
return ck_terms(todays_info, custom_info);
}
회고
예전에 파이썬으로 풀이하다 간신히 예제만 맞추고 틀렸다가 갑자기 바빠져서 못푼 문제였다.
지금 보니까 개월 수 계산하는 곳에서 문제가 있었다.
12보다 작아질 때 까지 한 것이아니라, 12보다 크면 한번하고 넘어갔다. 이부분 때문에 틀린듯
std::string 의 substr() 을 사용할 때, parameter로 두개가 들어갈 수 있는데 0번부터일때 자를때, 매번 0을 빼먹는다.
string.substr(0, 4) 를 하면 0부터 4개의 문자. string.substr(4) 하면 4번째 부터 끝까지로 자르니까 기억하자
'ps' 카테고리의 다른 글
99클럽 코테스터디 28일차 TIL 프로그래머스 - 스택수열 (0) | 2024.08.18 |
---|---|
99클럽 코테스터디 27일차 TIL 프로그래머스 - 공 이동 시뮬레이션 (0) | 2024.08.18 |
99클럽 코테스터디 25일차 TIL 프로그래머스 - 순위 (0) | 2024.08.15 |
99클럽 코테스터디 24일차 TIL 프로그래머스 - 가장 먼 노드 (0) | 2024.08.14 |
99클럽 코테스터디 23일차 TIL IPO (0) | 2024.08.14 |