본문 바로가기

코딩테스트/프로그래머스

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

728x90
반응형


🔗 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/150370?language=cpp

 

프로그래머스

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

programmers.co.kr

👩‍💻 코드

#include <string>
#include <vector>

using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    
    int year_today = stoi(today.substr(0,4)) - 2000;
    int month_today = stoi(today.substr(5,2));
    int day_today = stoi(today.substr(8,2));
    int today_number = (year_today * 12 * 28) + (month_today * 28) + day_today;
    
    for (int i=0; i<privacies.size(); i++){
        int year = stoi(privacies[i].substr(0,4)) - 2000;
        int month = stoi(privacies[i].substr(5,2));
        int day = stoi(privacies[i].substr(8,2));
        int number = (year * 12 * 28) + (month * 28) + day;
        int term = 0;
        char type = privacies[i].back();
        
        for (int j=0; j<terms.size(); j++){
            if(type == terms[j].front()){
                term = stoi(terms[j].substr(terms[j].find(" ")+1));
            }
        }
        
        number += term * 28;
        
        if(today_number >= number){
            answer.push_back(i+1);
        }
    }
    
    return answer;
}

📝 풀이

처음에는 날짜에서 년, 월, 일의 수를 각각 비교하는 방식으로 접근했다.

년도가 같거나 작으면 월을 비교하고, 월이 같거나 작으면 일을 비교하고...

하지만 제출하니 45% 정답률을 기록한 오답 코드...

게다가 코드는 알아 보기 어려울 정도로 덕지덕지 상태가 됐다.

그래서 문제를 찬찬히 다시 읽어 보니

'모든 달은 28일까지 있다고 가정합니다'

라는 정보가 강조 표시되어 있었다 ㅋㅋ!

여기서 힌트를 얻어 날짜 비교할 때 일수를 계산해서 비교하는 방식으로 접근하니 바로 통과!

(+)그리고 년도 조건은 2000년부터라 2000을 빼줬다.

728x90
반응형