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

Hanbi·2023년 2월 14일
0

Problem Solving

목록 보기
55/108
post-thumbnail

문제

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

풀이

전형적인 코테 문제 !!! 이 방법 익혀두면 문제열 쉽게 다룰 수 있을 것 같다.

🌟문자열 분리

  • stringstream으로 자료형에 맞게 문자열 분리
  • map 자료형 사용해서 key값 사용하기

이 문제는 year, month, day 각각 비교해도 되지만 쉽게 하기 위해서 day로 변환 후에 비교했다

코드

#include <string>
#include <vector>
#include <map>
#include <sstream>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    map<char, int> map;
    
    int year = stoi(today.substr(0, 4));
    int month = stoi(today.substr(5, 2));
    int day = stoi(today.substr(8, 2));
    int total = year * 12 * 28 + (month - 1) * 28 + day;
    
    for(int i=0; i<terms.size(); i++) {
        char c;
        int num;
        stringstream ss(terms[i]);
        ss >> c >> num;
        map[c] = num;
    }
    
    for(int i=0; i<privacies.size(); i++) {
        int y = stoi(privacies[i].substr(0, 4));
        int m = stoi(privacies[i].substr(5, 2));
        int d = stoi(privacies[i].substr(8, 2));
        char matching = privacies[i].back();
        m += map[matching];
        
        int tmp = y * 12 * 28 + (m - 1) * 28 + d - 1; // 문제에 의하면 2021.01.05 & 12달인 경우, 2022.01.04까지니까 날짜에서 -1 해야 함!
        
        if(total > tmp) // 오늘 날짜 기준으로 그거보다 작은 거 전부 폐기
            answer.push_back(i+1);
    }
    
    return answer;
}
profile
👩🏻‍💻

0개의 댓글