알고리즘

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

박카스마시며코딩 2023. 3. 29. 10:31

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

 

프로그래머스

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

programmers.co.kr

 

 

import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        int[] answer = {};
        List<Integer> list = new LinkedList<>();
        Map<Character,Integer> term = new HashMap<>();
        for(String str : terms){
            String[] temp = str.split(" ");
            char ch = temp[0].charAt(0);
            int month = Integer.parseInt(temp[1]);
            term.put(ch,month);
        }
        for(int i = 0 ; i < privacies.length ; i++){
            String privacy = privacies[i];
            if(check(today,privacy,term)){
                list.add(i+1);
            }
        }
        answer = new int[list.size()];
        for(int i = 0 ; i < list.size() ; i++){
            answer[i] = list.get(i);
        }
        System.out.println(list);
        return answer;
    }
    private static boolean check(String todayStr,String str , Map<Character,Integer> map){
        int today = strToDay(todayStr);
        String[] temp = str.split(" ");
        int day = strToDay(temp[0]);
        char type = temp[1].charAt(0);
        int period = map.get(type) * DAY_IN_MONTH;
        if(today >= day + period){
            return true;
        }
        return false;
    }
    private static final int DAY_IN_MONTH = 28;
    private static final  int DAY_IN_YEAR = DAY_IN_MONTH * 12;
    private static int strToDay(String str){
        String[] temp = str.split("\\.");
        int result = 0;
        result += Integer.parseInt(temp[0]) * DAY_IN_YEAR;
        result += Integer.parseInt(temp[1]) * DAY_IN_MONTH;
        result += Integer.parseInt(temp[2]);
        return result;
    }
}