https://school.programmers.co.kr/learn/courses/30/lessons/150370
고객의 약관 동의를 얻어서 수집된 1~n번으로 분류되는 개인정보 n개가 있습니다. 약관 종류는 여러 가지 있으며 각 약관마다 개인정보 보관 유효기간이 정해져 있습니다. 당신은 각 개인정보가 어떤 약관으로 수집됐는지 알고 있습니다. 수집된 개인정보는 유효기간 전까지만 보관 가능하며, 유효기간이 지났다면 반드시 파기해야 합니다.
예를 들어, A라는 약관의 유효기간이 12 달이고, 2021년 1월 5일에 수집된 개인정보가 A약관으로 수집되었다면 해당 개인정보는 2022년 1월 4일까지 보관 가능하며 2022년 1월 5일부터 파기해야 할 개인정보입니다.
당신은 오늘 날짜로 파기해야 할 개인정보 번호들을 구하려 합니다.
모든 달은 28일까지 있다고 가정합니다.
today는 "YYYY.MM.DD" 형태로 오늘 날짜를 나타냅니다.
1 ≤ terms의 길이 ≤ 20
1 ≤ privacies의 길이 ≤ 100
today와 privacies에 등장하는 날짜의 YYYY는 연도, MM은 월, DD는 일을 나타내며 점(.) 하나로 구분되어 있습니다.
파기해야 할 개인정보가 하나 이상 존재하는 입력만 주어집니다.
today | terms | privacies | result |
---|---|---|---|
"2022.05.19" | ["A 6", "B 12", "C 3"] | ["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"] | [1, 3] |
"2020.01.01" | ["Z 3", "D 5"] | ["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"] | [1, 4, 5] |
문제에서 모든 달은 28일 까지 있다고 가정하기 때문에, 실제 달력대로 계산하는 문제가 아니다. 연도 또한 2000년 부터 2022년 까지이므로, 날짜를 int형으로 변환하여 2000.01.01 이 후 지난 일 수로 나타낼 수 있다. 예를 들어, "2022.05.19"
를 (2022 - 2000) * (12 * 28) + 5 * 28 + 19 로 나타낸다. 나머지는 terms
와 privacies
의 문자열들을 dictionary 혹은 map으로 변환해주기만 하면 된다.
한 가지 주의해야했던 점은, Java에서 String.split() 메소드를 사용할 때, "." 대신에 "\."을 사용해야 했다.
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> answer = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
int todayInteger = dateToInteger(today);
for (String term : terms) {
map.put(term.split(" ")[0], Integer.parseInt(term.split(" ")[1]) * 28);
}
for (int i = 0; i < privacies.length; i++) {
int date = dateToInteger(privacies[i].split(" ")[0]);
int tm = map.get(privacies[i].split(" ")[1]);
if (date + tm <= todayInteger) answer.add(i + 1);
}
return answer.stream().mapToInt(i -> i).toArray();
}
private int dateToInteger(String date) {
System.out.println(date);
String[] temp = date.split("\\.");
int[] ymd = new int[3];
for (int i = 0; i < 3; i++) ymd[i] = Integer.parseInt(temp[i]);
return (ymd[0] - 2000) * (12 * 28) + ymd[1] * 28 + ymd[2];
}
}
def solution(today, terms, privacies):
answer = []
terms_json = {}
today = trans_date(today)
for term in terms:
t, m = term.split(' ')
terms_json[t] = int(m) * 28
for i, priv in enumerate(privacies):
date, t = priv.split(' ')
delta_month = int(terms_json[t])
_date = trans_date(date)
if _date + delta_month <= today:
answer.append(i + 1)
return answer
def trans_date(date):
ty, tm, td = [int(n) for n in date.split('.')]
return (ty - 2000) * 12 * 28 + tm * 28 + td