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

Pixel Dophin·2023년 6월 8일
0

프로그래머스

목록 보기
5/55
post-thumbnail

개인정보 수집 유효기간

문제링크

풀이

단순 구현문제
1. 오늘 날짜와 개인정보 수집 일자를 day로 환산
2. 두 날짜 사이의 기간을 구합니다.
3. 기간이 개인정보 보관 유효기간 보다 길다면 "오늘 날짜로 파기해야 할 개인정보 번호"이므로 answer에 순서대로 저장합니다.

코드

/*
 개인 정보 n개
 각 약관마다 개인정보 보관 유효기간이 정해져 있음
 
 모든 달은 28일까지 있다고 가정
*/
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        
        String[] split = today.split("\\.");
        int todayYear = Integer.parseInt(split[0]);
        int todayMonth = Integer.parseInt(split[1]);
        int todayDay = Integer.parseInt(split[2]);
        
        // 오늘 날짜를 day로 환산
        int totalDay = todayDay + 28*(todayMonth + 12*todayYear);
        
        int[] newTerms = new int[26];
        
        for (int i = 0; i < terms.length; i++){
            int termName = terms[i].charAt(0) - 'A'; // 약관 종류
            int termPeriod = Integer.parseInt(terms[i].split(" ")[1]); // 유효기간
            newTerms[termName]  = termPeriod;
        }
        
        Queue<Integer> queue = new ArrayDeque<Integer>();
        for (int i = 0; i < privacies.length; i++) {
            String[] splitPriv = privacies[i].split(" ");
            
            int curTermName = splitPriv[1].charAt(0) - 'A';
            String[] splitCurPriv = splitPriv[0].split("\\.");
            int curPrivYear = Integer.parseInt(splitCurPriv[0]);
            int curPrivMonth = Integer.parseInt(splitCurPriv[1]);
            int curPrivDay = Integer.parseInt(splitCurPriv[2]);
            int totalCurPrivDay = curPrivDay + 28*(curPrivMonth + 12*curPrivYear);
            
            // 오늘 날짜 - 개인정보 수집날짜 > 유효기간 인지 확인 => 유효기간 지남
            if ((totalDay - totalCurPrivDay) >= newTerms[curTermName] * 28) {
                queue.offer(i+1);
            }
        }
        
        int[] answer = new int[queue.size()];
        
        for (int i = 0; i< answer.length; i++) {
            answer[i] = queue.poll();
        }
        
        return answer;
    }
}

중복 코드 제거한 코드

/*
 개인 정보 n개
 각 약관마다 개인정보 보관 유효기간이 정해져 있음
 
 모든 달은 28일까지 있다고 가정
*/
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        
        // 오늘 날짜를 day로 환산
        int totalDay = getDays(today);
        
        int[] newTerms = new int[26];
        
        for (int i = 0; i < terms.length; i++){
            int termName = terms[i].charAt(0) - 'A'; // 약관 종류
            int termPeriod = Integer.parseInt(terms[i].split(" ")[1]); // 유효기간
            newTerms[termName]  = termPeriod;
        }
        
        Queue<Integer> queue = new ArrayDeque<Integer>();
        for (int i = 0; i < privacies.length; i++) {
            String[] splitPriv = privacies[i].split(" ");
            
            int curTermName = splitPriv[1].charAt(0) - 'A';
            int totalCurPrivDay = getDays(splitPriv[0]);
            
            // 오늘 날짜 - 개인정보 수집날짜 > 유효기간 인지 확인 => 유효기간 지남
            if ((totalDay - totalCurPrivDay) >= newTerms[curTermName] * 28) {
                queue.offer(i+1);
            }
        }
        
        int[] answer = new int[queue.size()];
        
        for (int i = 0; i< answer.length; i++) {
            answer[i] = queue.poll();
        }
        
        return answer;
    }
    
    public int getDays(String day) {
        String[] split = day.split("\\.");
        int dayYear = Integer.parseInt(split[0]);
        int dayMonth = Integer.parseInt(split[1]);
        int dayDay = Integer.parseInt(split[2]);
        
        // 날짜를 day로 환산
        return dayDay + 28*(dayMonth + 12*dayYear);
    }
}
profile
안녕 👋 성장하고픈 개발자 💻 입니다

0개의 댓글

관련 채용 정보