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

fsm12·2023년 6월 4일
0

프로그래머스

목록 보기
1/57
post-thumbnail

문제링크

문제 이해

[ 입력형태 / 조건 ]

inp_today
오늘날짜 문자열 | "2022.05.19"

inp_terms
약관의 유효기간을 담은 1차원 문자열 배열 | ["A 6", "B 12", ...] 약관종류 유효기간 | 길이는 최대 20

inp_privacies
수집된 개인 정보를 담은 1차원 문자열 배열 | ["2021.05.02 A", "2021.07.01 B", ...] 날짜 약관종류 | 길이는 최대 100

[ 문제 ]

2021년 1월 5일 => 12달 후 => 2022년 1월 4일까지 보관 가능
모든달은 28일까지

=> 파기해야 할 개인정보의 번호를 오름차순으로 1차원 배열에 담아 return

[ 풀이 ]

inp_terms를 해시맵으로 저장
위에서 매핑한 값을 inp_privacies와 비교해서 term < privacies 이면 해당 번호 추가



코드

import java.util.*;

class Solution {
    public int[] solution(String inp_today, String[] inp_terms, String[] inp_privacies) {
        Map <String, String> map = new HashMap<>();
        
        for (String terms : inp_terms) {
            map.put(terms.split(" ")[0], terms.split(" ")[1]);
        }

        int ty = Integer.parseInt(inp_today.split("\\.")[0]);
        int tm = Integer.parseInt(inp_today.split("\\.")[1]);
        int td = Integer.parseInt(inp_today.split("\\.")[2]);
        
        List<Integer> ans_list = new ArrayList<>();
        for (int p=0; p < inp_privacies.length; p++) {
            String pastDate = inp_privacies[p].split(" ")[0];
            
            int valid_gap = Integer.parseInt(map.get(inp_privacies[p].split(" ")[1]))*28;
            int tp_gap = (ty - Integer.parseInt(pastDate.split("\\.")[0]))*28*12
                + (tm - Integer.parseInt(pastDate.split("\\.")[1]))*28
                + (td - Integer.parseInt(pastDate.split("\\.")[2]));

            if (tp_gap >= valid_gap) {
                ans_list.add(p+1);
            }
        }
        
        int[] answer = new int[ans_list.size()];
        int idx = 0;
        for(int ans : ans_list){
            answer[idx++] = ans;
        }
        return answer;
    }
}

TIP : 달의 일수가 고정적인 날짜나 시간을 다뤄야 하는 문제는 가장 작은 단위(일, 초)로 연산하는 것이 편함!

0개의 댓글