프로그래머스

LV1 [KAKAO] 개인정보 수집 유효기간

whiporithm 2023. 8. 22. 23:10


문제

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

 

프로그래머스

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

programmers.co.kr

풀이

 

문자열을 파싱한다음 날짜를 비교하면 된다.

 

주의할점은 '달' 만큼 더해준다음, '일' 을 하루 빼줘야하는것이다.

 

"Z 1"

"2020.01.02 Z" 일 때 유효기간은 2020.02.02가 아니라, 2020.02.01 이다.

 

따라서 여기에 대한 처리를 해줘야한다. day가 1이면 month를 감소시켜줘야한다.

day도 month도 1이라면 year까지 감소시켜줘야한다. (2020.01.01 => 2019.12.28)

 

다음 주의할점은 month가 12를 넘는경우이다. 12를 넘기면 year을 1 증가시켜준다. 12일때는 그냥 둬야한다.
(나는 처음에 year += month%12 로 작성했는데 이 경우에 month가 12의 배수이면 month가 0이 된다는 오류가 있었다.)

 

코드

import java.util.*;

class Solution {
    public List<Integer> solution(String today, String[] terms, String[] privacies) {
       
        List<Integer> answer = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        String[] res = today.split("\\.");
        Integer t_year = Integer.parseInt(res[0]);
        Integer t_month = Integer.parseInt(res[1]);
        Integer t_day = Integer.parseInt(res[2]);
        
        for(String term : terms){
            map.put(term.split(" ")[0], Integer.parseInt(term.split(" ")[1]));
        }
        
        Integer cnt = 0;
        
        for(String privacy : privacies){
            cnt++;
            
            String temp = privacy.split(" ")[0];
            String type = privacy.split(" ")[1];
            
            Integer year = Integer.parseInt(temp.split("\\.")[0]);
            Integer month = Integer.parseInt(temp.split("\\.")[1]);
            Integer day = Integer.parseInt(temp.split("\\.")[2]);
            
            month += map.get(type);
            
            while(month > 12){
                year += 1;
                month -=12;
            }
            
            // 하루 깎아줘야함.
            if(day == 1){
                if(month == 1){
                    year -= 1;
                    month = 12;
                    day = 28;
                } else{
                    month -= 1;
                    day = 28;
                }
            } else{
                day -= 1;
            }
            
            // System.out.println(year + ", " + month + ", " + day);
                        
            if(t_year > year){
                answer.add(cnt);
            } else if(t_year < year){ 
                continue;
            } else{                 
                if(t_month > month){
                    answer.add(cnt);
                } else if(t_month < month){
                    continue;
                } else{                  
                    if(t_day > day){
                         answer.add(cnt);
                    } 
                }
            }
        }
    
        return answer;
    }
}

 

후기

 

이런 문제가 은근 까다롭다. 레벨1이지만 뭐 하나 잘못걸리면 캐치하기 어려운..

 

그리고 나는 되게 틀에박힌 방식으로 풀었는데, "년도 달 일" 을 모두 "일" 로 바꾸어 비교하는 코드가 있었다.
파싱까지 한 다음 (year * 12 + month) * 28 + day 방식으로 모두 "일"로 바꾸어 계산하는 것이다.

이게 200만배는 훨씬 깔끔하고 좋은 거 같다. 날짜 비교가 나온다면 이 방식을 꼭 고려해봐야겠다.

'프로그래머스' 카테고리의 다른 글

LV3 [KAKAO] 합승 택시 요금  (0) 2023.08.24
LV3 [KAKAO] 경주로 건설  (0) 2023.08.23
LV3 [KAKAO] 징검다리 건너기  (0) 2023.08.22
LV3 [KAKAO] 보석 쇼핑  (0) 2023.08.21
LV3 [KAKAO] 불량 사용자  (0) 2023.08.18