프로그래밍/프로그래머스

Lv1_[개인정보 수집 유효기간, C++] 풀이 및 알고리즘 정리 - 프로그래머스

워킹독 2023. 2. 8. 09:56
728x90

 

 

프로그래머스

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

programmers.co.kr

문제 내용, 조건, 예시는 프로그래머스 사이트 참조


알고리즘

1.날짜 비교

 날짜의 단순 크기를 비교하는 간단한 방법은 년.월.일. 모두를 '일' 기준으로 풀어서 쓰는 것
1달을 28일로 기준이라면 (1월=28일), (1년=12월=12*28일=336)으로 풀어 쓸 수 있습니다.

YYYY.MM.DD를 계산식으로 만들면 (YYYY*12*28) + ((MM-1)*28) + DD 이 됨

today 2022.05.19를 계산하면
(2022*12*28) + ((5-1)*28) + 19 = 679,523
나온 값으로 문제에서 주어진 날짜와 비교하면 된다.


2.문자열 토큰 분리

문자열 분리 방법은 여러가지가 있지만 사용한 방법은 stringstream과 getline을 이용한 것이다.
문자열을 분리하면 날짜와 약관종류가 나오고 그것을 날짜 계산식에 넣어서 값을 뽑아준다

ex) 2021.05.02 A 6달

  • day 2021.05.02 → 679,170
  • A 6달 → 168

위의 값까지 모두 출력했다면 비교를 해주면 된다.

(today - day >= 약관종류) 가 성립하면 유효기간을 넘어선 것이다.

  • 679,523 - 679,170 = 353
  • 353 > 168(6달)  유효기간을 넘었음


위와 같은 방식으로 풀어주면 완료!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string>
#include <vector>
#include <sstream>
#include <map>
 
#define MONTH 12
#define DAY 28
 
using namespace std;
 
int calculation_DAY(string today) {
    // YY*(12*28) + (MM-1)*28 + DD
    stringstream ss(today);
    string day[3];
    getline(ss, day[0], '.');
    getline(ss, day[1], '.');
    getline(ss, day[2], '.');
    return (stoi(day[0])*MONTH*DAY + (stoi(day[1])-1)*DAY + stoi(day[2]));
}
 
pair<stringstring> separation(const string &str) {
    pair<stringstring> p;
    
    stringstream ss(str);
    getline(ss, p.first, ' ');
    getline(ss, p.second, ' ');
 
    return p;
}
 
vector<int> solution(string today, vector<string> terms, vector<string> privacies) {
    vector<int> answer;
 
    // today convert
    int today_integer = calculation_DAY(today);
 
    // terms save
    map<stringint> m;
    for(auto item : terms) {
        auto p = separation(item);
        m[p.first] = stoi(p.second);
    }
 
    // privacies
    for(int i = 0; i < privacies.size(); i++) {
        auto p = separation(privacies[i]);
 
        int day_integer = calculation_DAY(p.first);
        int type_day = m[p.second] * DAY;
 
        if(today_integer - day_integer >= type_day) {
            answer.emplace_back(i+1);
        }
    }
 
    return answer;
}
cs

 

↓↓↓↓↓↓ 유익했다면 하트 뿅 ♥ ↓↓↓↓↓↓

 

 

728x90
반응형