와이유스토리

[문자열] 프로그래머스 개인정보 수집 유효기간 C++, Python 본문

코딩테스트/문자열

[문자열] 프로그래머스 개인정보 수집 유효기간 C++, Python

유(YOO) 2023. 1. 18. 13:15

 

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

 

프로그래머스

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

programmers.co.kr

#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    
    unordered_map<char, int> mp;
    for(auto t : terms) mp[t[0]] = stoi(t.substr(2, t.length() - 1));
    
    int year = stoi(today.substr(0, 4));
    int month = stoi(today.substr(5, 7));
    int date = stoi(today.substr(8, 10));
    int num = year * 12 * 28 + month * 28 + date;
    
    for(int i=0; i<privacies.size(); i++) {
        int y = stoi(privacies[i].substr(0, 4));
        int m = stoi(privacies[i].substr(5, 7));
        int d = stoi(privacies[i].substr(8, 10));
        char t = privacies[i][11];
        
        int n = y * 12 * 28 + m * 28 + d + mp[t] * 28 - 1;
        if (n < num) answer.push_back(i+1);
    }
    
    return answer;
}
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    
    unordered_map<char, int> mp;
    for(auto t : terms) {
        stringstream ss(t);
        char type;
        int period;
        ss >> type >> period;
        mp[type] = period;
    }
    
    int year = stoi(today.substr(0, 4));
    int month = stoi(today.substr(5, 7));
    int date = stoi(today.substr(8, 10));
    
    for(int i=0; i<privacies.size(); i++) {
        int y = stoi(privacies[i].substr(0, 4));
        int m = stoi(privacies[i].substr(5, 7));
        int d = stoi(privacies[i].substr(8, 10));
        char t = privacies[i][11];
        
        m += mp[t];
        d--;
        if (m > 12) {
            y += m/12;
            m = m%12;
        }
        if (d == 0) {
            m--;
            d = 28;
        }
        if (m == 0) {
            m = 12;
            y--;
        }
        
        if (y < year) answer.push_back(i+1);
        else if (y == year){
            if (m < month) answer.push_back(i+1);
            else if (m == month) {
                if (d < date) answer.push_back(i+1);
            }
        }
    }
    
    return answer;
}
def solution(today, terms, privacies):
    answer = []
    period = int(today[2:4])*12*28+int(today[5:7])*28+int(today[8:10])
    dic =  {i[0] : int(i[2:]) for i in terms}
    for i, p in enumerate(privacies):
        comp = int(p[2:4])*12*28+int(p[5:7])*28+int(p[8:10])
        if comp+dic[p[-1]]*28 <= period:
            answer.append(i+1)
    return answer
Comments