문제/프로그래머스

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

icodesiuuuu 2025. 1. 2. 16:31

문제

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 


 

문제 개요

이 문제는 개인정보의 보관 유효기간이 정해진 약관을 기준으로, 오늘 날짜를 기준으로 파기해야 할 개인정보를 찾아내는 문제입니다. 각 약관에 따른 유효기간과 개인정보 수집 날짜가 주어지며, 오늘 날짜 이후 유효기간이 지난 개인정보는 파기 대상이 됩니다. 모든 날짜 계산은 달 수를 기준으로 하고, 모든 달이 28일까지 있다고 가정합니다.

 

 

문제 접근 방법

  1. 날짜를 정수형으로 변환
    날짜 계산을 단순화하기 위해 YYYY.MM.DD 형식의 날짜를 총 일수로 변환하였다. 연도는 12개월로, 월은 28일로 계산하여 일 단위로 나타낸다.
    • 예) 2021.05.02 → (2021 * 12 * 28) + (5 * 28) + 2
  2. 약관 정보 맵핑
    각 약관의 보관 유효기간을 HashMap에 저장하여, 약관 종류에 따라 유효기간을 빠르게 참조할 수 있도록 구현하였다.
    • 예) A 6 → map.put("A", 6 * 28)
  3. 개인정보 검증
    각 개인정보의 수집 날짜와 약관 종류를 통해 유효기간을 계산한 후, 오늘 날짜와 비교하여 파기 여부를 판단하였다.
    • 만약 (오늘 날짜 - 수집 날짜) >= 약관 유효기간이면 해당 개인정보는 파기 대상.

 

 

코드

import java.util.*;

class Solution {
    static HashMap<String, Integer> map = new HashMap<>();
    static List<Integer> list = new ArrayList<>();
    
    public int[] solution(String today, String[] terms, String[] privacies) {
        int todayTime = StringToInt(today);
        makeMap(terms);
        checkPossible(todayTime, privacies);
        
        int[] answer = new int[list.size()];
        for(int i=0; i<list.size(); i++) {
            answer[i] = list.get(i);
        }
        
        return answer;
    }
    
    public void checkPossible(int todayTime, String[] privacies) {
        for(int i=0; i<privacies.length; i++) {
            String[] arr = privacies[i].split(" ");
            int curTime = StringToInt(arr[0]);
            String curKind = arr[1];
            
            if(isPossible(curTime, todayTime, curKind)) list.add(i+1);
        }
    }
    
    public boolean isPossible(int curTime, int todayTime, String curKind) {
        if(todayTime - curTime >= map.get(curKind)) return true;
        return false;
    }
    
    public void makeMap(String[] terms) {
        for(String s : terms) {
            String[] arr = s.split(" ");
            
            int time = Integer.parseInt(arr[1]) * 28;
            map.put(arr[0], time);
        }
    }
    
    public int StringToInt(String s) {
        String[] arr = s.split("\\.");
        int n = 0;
        
        n += Integer.parseInt(arr[0]) * 12 * 28;
        n += Integer.parseInt(arr[1]) * 28;
        n += Integer.parseInt(arr[2]);
        
        return n;
    }
}