✏️아이디어
- 날짜에 대한 문자열에서 .을 제거
- 제거한 문자열끼리 크기 비교를 해서 판단해도 될것 같다
-> "20200101" > "20190101"
그럼 유효기간만큼 더해줄때는 어떻게 해?
- 그러게 숫자로 일단 변환하자
0. today는 . 빼고 문자열로 저장해두기
1. terms를 공백 기준으로 자르고 HashMap에 저장해두자
2. privacies를 하나씩 정리하자
- 공백을 기준으로 자르고 후자인 약관종류는 terms에서 숫자로 바꿔오자
- 날짜는 .을 기준으로 잘라서 모두 숫자로 변환하자
- 수집 기간에 맞추도록 -1일을 해준 뒤 term 만큼 날짜를 더해주자
- 완성한 일자가 today를 넘는다면 현재 index를 저장하자
💡풀이 1
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
Map<String, Integer> termMap = new HashMap<>();
for(String term : terms) {
String[] arr = term.split(" ");
termMap.put(arr[0], Integer.parseInt(arr[1]));
}
today = today.replace(".", "");
List<Integer> answer = new ArrayList<>();
for(int index = 0; index < privacies.length; index++) {
String[] arr = privacies[index].split(" ");
int term = termMap.get(arr[1]);
String[] date = arr[0].split("\\.");
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
if(day != 1) {
day--;
} else {
day = 28;
month--;
if(month == 0) {
month = 12;
year--;
}
}
for(int i = 1; i <= term; i++) {
month++;
if(month > 12) {
month = 1;
year++;
}
}
String str = year + String.format("%02d", month) + String.format("%02d", day);
if(str.compareTo(today) < 0) {
answer.add(index + 1);
}
}
return answer.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}
💡풀이 2
- 주어진 날짜들을 day를 기준으로 변환하여 값을 비교
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> answer = new ArrayList<>();
Map<String, Integer> termMap = new HashMap<>();
for(String term : terms) {
String[] arr = term.split(" ");
termMap.put(arr[0], Integer.parseInt(arr[1]));
}
int now = getDate(today);
for(int index = 0; index < privacies.length; index++) {
String[] arr = privacies[index].split(" ");
int term = termMap.get(arr[1]);
int priv = getDate(arr[0]);
if(now >= priv + term * 28) {
answer.add(index + 1);
}
}
return answer.stream()
.mapToInt(Integer::intValue)
.toArray();
}
private int getDate(String date) {
String[] arr = date.split("\\.");
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
int day = Integer.parseInt(arr[2]);
return (year * 12 * 28) + (month * 28) + day;
}
}
📖새로 배운 부분
- 날짜 비교는 일(day) 단위로 변환해서 처리한다.
여러 날짜 정보를 간단하게 비교하려면 연, 월, 일을 모두 일(day)로 바꾼 뒤 계산하는 것이 훨씬 쉽다
이렇게 하면 코드가 간결해지고, 날짜 비교도 빠르게 할 수 있다 - split에서 .은 반드시 \\로 처리하자
점(.)은 정규식에서 '모든 문자'라는 뜻이기 때문에, 오동작을 막으려면 꼭 (\\.) 이렇게 역슬래시로 실제 문자라고 명시해주자
GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.
프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다. Contribute to okjunghyeon/Programmers_CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Programmers' 카테고리의 다른 글
[프로그래머스, Java] 유연근무제 (0) | 2025.07.29 |
---|---|
[프로그래머스, Java] 바탕화면 정리 (0) | 2025.07.29 |
[프로그래머스, Java] 신규 아이디 추천 (0) | 2025.07.29 |
[프로그래머스, Java] 성격 유형 검사하기 (0) | 2025.07.21 |
[프로그래머스, Java][카카오 인턴] 키패드 누르기 (0) | 2025.07.21 |