[프로그래머스] LV1. 개인정보 수집 유효기간

인스·2025년 7월 11일

💡 풀이

  • String 날짜를 일수로 변경해주는 함수 => toDays
  • 약관 종류와 유효기간 Map에 담기
  • privacies 하나씩 확인하면서 개인정보 수집 일자 + 유효기간 일수 구하기
  • 오늘 날짜 일수와 비교해서 오늘 날짜가 더 크면 만료기간이 지났으므로 정답 리스트에 담기
  • ⭐ "2021.01.11" . 기준으로 나눌 때 split(".") 아닌 split("\.")으로 해야함
    -> 전자로 하면 .(dot)을 모든 문자를 기준으로 나뉨
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        int todays = toDays(today); // 오늘 날짜 일수로 변경 
        ArrayList<Integer> res = new ArrayList<>();
        
        // 약관 종류와 유효기간 map에 담기
        HashMap<String, Integer> termMap = new HashMap<>();
        for(String term : terms){
            termMap.put(term.split(" ")[0], Integer.parseInt(term.split(" ")[1]));
        }
        
        for(int i = 0; i<privacies.length; i++){
            String[] sp = privacies[i].split(" ");
            // 개인정보 수집 일자 + 유효기간
            int expDays = toDays(sp[0]) + termMap.get(sp[1]) * 28;
            
            if (todays >= expDays) res.add(i+1);
        }
        
        // list에 담은 정답 배열로 옮기기
        int[] answer = new int[res.size()];
        int idx = 0;
        for(int r : res){
            answer[idx++] = r;
        }
        return answer;
    }
    
    // 일수로 변경 메소드
    public int toDays(String date){
        String[] d = date.split("\\.");
        int year = Integer.parseInt(d[0]);
        int month = Integer.parseInt(d[1]);
        int day = Integer.parseInt(d[2]);
        
        return year * 12 * 28 + month * 28 + day;
    }
}
profile
💻💡👻

0개의 댓글