문제 링크입니다: https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문자열 파싱만 잘하면 쉽게 풀 수 있는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영합니다! 질문 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 주문량이 많은 아이스크림들 조회하기 (0) | 2023.01.09 |
---|---|
[Programmers] 조건에 맞는 도서와 저자 리스트 출력하기 (2) | 2023.01.08 |
[Programmers] 카테고리 별 도서 판매량 집계하기 (0) | 2023.01.05 |
[Programmers] 조건에 맞는 도서 리스트 출력하기 (0) | 2023.01.04 |
[Programmers] 저자 별 카테고리 별 매출액 집계하기 (3) | 2023.01.04 |