[코딩테스트] [프로그래머스] 개인정보 수집 유효기간

김민정·2025년 9월 24일
1

코딩테스트

목록 보기
31/33
post-thumbnail

문제

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


풀이와 주의점

풀이

  1. '.' 기준으로 오늘 날짜 문자열을 나눠 벡터에 저장해준다.
    1-1. 띄어쓰기 기준으로 문자열을 나눠 약관 종류를 key, 유효기간을 value로 map에 저장해준다.
    1-2. 띄어쓰기 기준으로 문자열을 나눠 수집 일자 / 약관 종류를 나누고, 다시 수집 일자를 '.' 기준으로 나눠 벡터에 저장해준다.

  2. 오늘 날짜, 약관 종류에 따른 유효기간, 수집 일자를 순차적으로 isDestroy에 넣어서 계산한다.

주의점

  1. 모든 달은 28일이라고 가정한다.

코드

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

using namespace std;

bool isDestroy(vector<int>& day, int term, vector<int>& today)
{
    day[0] += term/12;
    day[1] += term%12;
    
    if(day[1] > 12)
    {
        day[0] += day[1]/12;
        day[1] = day[1]%12;
    }
    
    if(day[2] > 1)
    {
        day[2] -=1;
    }
    else if(day[2] == 1)
    {
        day[1] -=1;
        day[2] = 28;
    }
    
    if(day[0] == today[0])
    {
        if(day[1] == today[1])
        {
            return day[2] >= today[2] ? false : true;
        }
        else
        {
            return day[1] > today[1] ? false : true;
        }
    }

    return day[0] > today[0] ? false : true;
}

vector<int> solution(string today, vector<string> terms, vector<string> privacies) 
{
    stringstream ss(today);
    string temp;
    vector<int> todayInt;
    while(getline(ss, temp, '.'))
    {
        todayInt.push_back(stoi(temp));
    }
    
    map<string, int> termsMap;
    for(int i=0; i<terms.size(); i++)
    {
        ss.str(terms[i]);
        ss.clear();
        string key;
        int value;
        ss >> key >> value;
        termsMap.insert({key, value});
    }
    
    vector<int> answer;
    for(int i=0; i<privacies.size(); i++)
    {
        ss.str(privacies[i]);
        ss.clear();
        string day;
        string key;
        ss >> day >> key;
        
        ss.str(day);
        ss.clear();
        vector<int> dayInt;
        while(getline(ss, temp, '.'))
        {
            dayInt.push_back(stoi(temp));
        }
        
        if(isDestroy(dayInt, termsMap[key], todayInt))
            answer.push_back(i+1);
    }
    
    return answer;
}

다른 풀이와 코드

  1. 모든 날짜를 일자 기준으로 변환해서 손쉽게 계산한 코드
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
vector<int> solution(string today, vector<string> terms, vector<string> privacies) 
{
    vector<int> answer;
    int year = stoi(today.substr(0, 4));
    int month = stoi(today.substr(5, 2));
    int days = stoi(today.substr(8));
    int todayD = (year)*12 * 28 + (month - 1) * 28 + days;

    vector<int> ar(privacies.size());
    map<char, int> mp;
    for (int i = 0; i < terms.size(); i++) 
    {
        stringstream ss(terms[i]);
        char c;
        int month;
        ss >> c >> month;
        mp[c] = month;
    }
    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 c = privacies[i].back();
        ar[i] = (y)*12 * 28 + (m - 1) * 28 + d + mp[c] * 28 - 1;
    }
    for (int i = 0; i < ar.size(); i++) 
    {
        if (ar[i] < todayD) 
        {
            answer.push_back(i + 1);
        }
    }
    return answer;
}
profile
📝 공부노트

0개의 댓글