개인정보 수집 유효기간

개굴이·2023년 9월 25일
0

코딩테스트

목록 보기
32/58
post-thumbnail

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

오늘 날짜를 의미하는 문자열 today, 약관의 유효기간을 담은 1차원 문자열 배열 terms와 수집된 개인정보의 정보를 담은 1차원 문자열 배열 privacies가 매개변수로 주어집니다. 이때 파기해야 할 개인정보의 번호를 오름차순으로 1차원 정수 배열에 담아 return 하도록 solution 함수를 완성해 주세요.

소스

import java.util.*;
import java.time.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        int[] answer = new int[privacies.length];
        int cnt = 0;
        LocalDate todayDate = LocalDate.parse(today.replace(".", "-"));
        Map<String, Integer> termMap = new HashMap<>();
        for(int i = 0; i < terms.length; i++) {
            String[] term = terms[i].split(" ");
            termMap.put(term[0], Integer.parseInt(term[1]));
        }
        for(int i = 0; i < privacies.length; i++) {
            String[] now = privacies[i].split(" ");
            LocalDate day = LocalDate.parse(now[0].replace(".", "-"));         
            if(day.plusMonths(termMap.get(now[1])).compareTo(todayDate) <= 0) {
                answer[cnt++] = i + 1;
                System.out.println(now[0] + termMap.get(now[1]) + todayDate);
            }
        }
        int result [] = new int[cnt];
        for(int i = 0; i < cnt; i++)
            result[i] = answer[i];
        return result;
    }
}

0개의 댓글