๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.1 : ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ์œ ํšจ๊ธฐ๊ฐ„ (์นด์นด์˜ค ๊ธฐ์ถœ)

728x90

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

 

2023 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฝ”ํ…Œ ๊ธฐ์ถœ

 

๋ฌธ์ œ ์„ค๋ช…

๊ณ ๊ฐ์˜ ์•ฝ๊ด€ ๋™์˜๋ฅผ ์–ป์–ด์„œ ์ˆ˜์ง‘๋œ 1~n๋ฒˆ์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ๊ฐœ์ธ์ •๋ณด n๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ฝ๊ด€ ์ข…๋ฅ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์žˆ์œผ๋ฉฐ ๊ฐ ์•ฝ๊ด€๋งˆ๋‹ค ๊ฐœ์ธ์ •๋ณด ๋ณด๊ด€ ์œ ํšจ๊ธฐ๊ฐ„์ด ์ •ํ•ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ๊ฐ ๊ฐœ์ธ์ •๋ณด๊ฐ€ ์–ด๋–ค ์•ฝ๊ด€์œผ๋กœ ์ˆ˜์ง‘๋๋Š”์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ง‘๋œ ๊ฐœ์ธ์ •๋ณด๋Š” ์œ ํšจ๊ธฐ๊ฐ„ ์ „๊นŒ์ง€๋งŒ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚ฌ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํŒŒ๊ธฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, A๋ผ๋Š” ์•ฝ๊ด€์˜ ์œ ํšจ๊ธฐ๊ฐ„์ด 12 ๋‹ฌ์ด๊ณ , 2021๋…„ 1์›” 5์ผ์— ์ˆ˜์ง‘๋œ ๊ฐœ์ธ์ •๋ณด๊ฐ€ A์•ฝ๊ด€์œผ๋กœ ์ˆ˜์ง‘๋˜์—ˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐœ์ธ์ •๋ณด๋Š” 2022๋…„ 1์›” 4์ผ๊นŒ์ง€ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ 2022๋…„ 1์›” 5์ผ๋ถ€ํ„ฐ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด์ž…๋‹ˆ๋‹ค.
๋‹น์‹ ์€ ์˜ค๋Š˜ ๋‚ ์งœ๋กœ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด ๋ฒˆํ˜ธ๋“ค์„ ๊ตฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  ๋‹ฌ์€ 28์ผ๊นŒ์ง€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ 2022.05.19์ผ ๋•Œ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

 

์•ฝ๊ด€ ์ข…๋ฅ˜์œ ํšจ๊ธฐ๊ฐ„
A 6 ๋‹ฌ
B 12 ๋‹ฌ
C 3 ๋‹ฌ
๋ฒˆํ˜ธ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ์ผ์ž์•ฝ๊ด€ ์ข…๋ฅ˜
1 2021.05.02 A
2 2021.07.01 B
3 2022.02.19 C
4 2022.02.20 C
  • ์ฒซ ๋ฒˆ์งธ ๊ฐœ์ธ์ •๋ณด๋Š” A์•ฝ๊ด€์— ์˜ํ•ด 2021๋…„ 11์›” 1์ผ๊นŒ์ง€ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚ฌ์œผ๋ฏ€๋กœ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด์ž…๋‹ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ๊ฐœ์ธ์ •๋ณด๋Š” B์•ฝ๊ด€์— ์˜ํ•ด 2022๋…„ 6์›” 28์ผ๊นŒ์ง€ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์•„์ง ๋ณด๊ด€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์„ธ ๋ฒˆ์งธ ๊ฐœ์ธ์ •๋ณด๋Š” C์•ฝ๊ด€์— ์˜ํ•ด 2022๋…„ 5์›” 18์ผ๊นŒ์ง€ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚ฌ์œผ๋ฏ€๋กœ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด์ž…๋‹ˆ๋‹ค.
  • ๋„ค ๋ฒˆ์งธ ๊ฐœ์ธ์ •๋ณด๋Š” C์•ฝ๊ด€์— ์˜ํ•ด 2022๋…„ 5์›” 19์ผ๊นŒ์ง€ ๋ณด๊ด€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์•„์ง ๋ณด๊ด€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด ๋ฒˆํ˜ธ๋Š” [1, 3]์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฌธ์ž์—ด today, ์•ฝ๊ด€์˜ ์œ ํšจ๊ธฐ๊ฐ„์„ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด terms์™€ ์ˆ˜์ง‘๋œ ๊ฐœ์ธ์ •๋ณด์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด privacies๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ํŒŒ๊ธฐํ•ด์•ผ ํ•  ๊ฐœ์ธ์ •๋ณด์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

 


 

์นด์นด์˜ค ๊ธฐ์ถœ ์ค‘ level 1 ๋ฌธ์ œ์ด๋‹ค.

๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ์ด์™ธ์˜ ํŠน๋ณ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ‚ฌ์ด ํ•„์š”ํ•˜์ง„ ์•Š์ง€๋งŒ, ์€๊ทผํžˆ ๊ตฌํ˜„์ด ๊นŒ๋‹ค๋กœ์› ๋‹ค.

 

12๊ฐœ์›”์ด ๋„˜์–ด๊ฐ€๋ฉด 1๋…„์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ,

์ด ๋•Œ 12๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๊ฐ’, ์ฆ‰ month ๊ฐ’์ด 0์ด ๋˜๋ฉด ๊ธฐ์กด privacies์˜ MM๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•ด์ค˜์•ผ ํ•œ๋‹ค!!

์ด ๋ถ€๋ถ„์„ ๋†“์ณ์„œ ํ•œ๋ฒˆ ํ‹€๋ฆผใ… 

 

 

๋‚˜์˜ ํ’€์ด

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
    vector<char> termA; // ์•ฝ๊ด€ ์ข…๋ฅ˜
    vector<int> term1; // ์•ฝ๊ด€ ์œ ํšจ๊ธฐ๊ฐ„ ๋‹ฌ ์ˆ˜
    
    int todayY = stoi(today.substr(0,4));
    int todayM = stoi(today.substr(5,2));
    int todayD = stoi(today.substr(8,2));
    
    for(int i=0; i<terms.size();++i){ // ์•ฝ๊ด€ ์ข…๋ฅ˜์™€ ๊ธฐ๊ฐ„ ๋ถ„๋ฆฌ
        termA.push_back(terms[i][0]);
        terms[i].replace(0,2,"");
        term1.push_back(stoi(terms[i]));
    }
    
    for(int i=0; i<privacies.size(); ++i){
        char kindof=privacies[i][11];
        int pY = stoi(privacies[i].substr(0,4));
        int pM = stoi(privacies[i].substr(5,2));
        int pD = stoi(privacies[i].substr(8,2));
        
        int index = 0;
        int year = 0;
        int month = 0;
        for(int j=0; j<termA.size(); ++j){
            if(termA[j] == kindof) {
                index = j;
                break;
            }
        }
        year = term1[index]/12;
        month = term1[index]%12;
        year += pY;
        month += pM;
        if(month > 12){ // 24
            year += month/12; // +2
            month = month%12; // 0
        }
        if(month==0) month += pM;
        cout <<year<<" "<<month<<"\n";
        if(year < todayY) answer.push_back(i+1);
        else if(year == todayY){
            if(month< todayM) answer.push_back(i+1);
            else if(month==todayM){
                if(pD-1 < todayD) answer.push_back(i+1);
            }
        }
        
    }
    return answer;
}

 

728x90