- 프로래머스 Lv1 개인정보 수집 유효기간 문제
- 해당 문제의 난이도 상승 원인
- 날짜값을 모두 일 기준으로 변환 후, 추후에 또 다시 날짜로 재변환 해야함
- 모든 달이 28일이라는 제한점
- 해당 문제를 해결한 방법
- 날짜를 모두 일 기준으로 변환하여 유효 개월값과 비교해 조건에 따라 처리
- 처음 방식에서 변경한 방식
- 처음
- 연, 월, 일을 각각 계산하여 비교하려 시도하였으나 코드의 복잡성이 증가
- 변경
- 최대한 단순하게 변환한 후에 마지막에 원본 형태로 재변환 함
- 문제의 핵심
import java.util.*;
class PersonalPeriod {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> trash = new ArrayList<>();
// terms 전처리
Map<String, Integer> validTerm = new HashMap<>();
for(String t : terms){
int space = t.indexOf(' ');
validTerm.put(t.substring(0, space), Integer.parseInt(t.substring(space + 1)));
}
// privacies를 기준으로 순혼
for(int i = 0; i < privacies.length; i++){
int first = privacies[i].indexOf('.');
int second = privacies[i].indexOf('.', first + 1);
int space = privacies[i].indexOf(' ');
String select = privacies[i].substring(space + 1);
int conDay =
Integer.parseInt(privacies[i].substring(0, first)) * 12 * 28 +
Integer.parseInt(privacies[i].substring(first + 1, second)) * 28 +
validTerm.get(select) * 28 +
Integer.parseInt(privacies[i].substring(second + 1, space)) - 1;
int conToday =
Integer.parseInt(today.substring(0, first)) * 12 * 28 +
Integer.parseInt(today.substring(first + 1, second)) * 28 +
Integer.parseInt(today.substring(second + 1, space)) - 1;
if(conDay <= conToday){
trash.add(i + 1);
}
}
int[] answer = trash.stream().mapToInt(Integer::intValue).toArray();
return answer;
}
public static void main(String[] args) {
String today = "2022.05.19";
String[] terms = {"A 6", "B 12", "C 3"};
String[] privacies = {"2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"};
PersonalPeriod p = new PersonalPeriod();
System.out.println("결과 : " + Arrays.toString(p.solution(today, terms, privacies)));
}
}