본문 바로가기
알고리즘

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

by 상후 2023. 8. 4.
728x90
반응형

 

 

https://github.com/ROUTINE-STUDY/Algorithm

알고리즘 스터디를 진행하고 있습니다. 😊
초보들로 구성되어있으며, 열심히 풀어보고 풀이 방식을 공유하고 피드백을 해주는 스터디입니다.
참여 문의는 댓글 혹은 GitHub 주소를 참고해주세요.

문제 출처 :  https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

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

programmers.co.kr

 

 

내 코드(JAVA)

 

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Solution {
    
    // key : 약관, value : 유효기간
    private HashMap<String, Integer> termMap = new HashMap<>();
    
    public int[] solution(String today, String[] terms, String[] privacies) {
        List<Integer> list = new ArrayList<>();

        // 약관 종류 및 유효기간 Map 세팅
        for (String term : terms) {
            String[] termArray = term.split(" ");
            termMap.put(termArray[0], Integer.parseInt(termArray[1]));
        }

        // 개인정보 정보 처리
        for (int i = 0; i < privacies.length; i++) {
            String[] privacyArray = privacies[i].split(" ");
            String dateStr = privacyArray[0]; // 날짜
            String termStr = privacyArray[1]; // 약관종류

            LocalDate calcDate = calcLocalDate(dateStr, termStr); // 유효기간 계산
            LocalDate todayDate = LocalDate.of(Integer.parseInt(today.substring(0, 4)), Integer.parseInt(today.substring(5, 7)), Integer.parseInt(today.substring(8, 10)));

            if (calcDate.isBefore(todayDate)) {
                list.add(i + 1);
            }
        }

        int[] answer = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i);
        }

        return answer;
    }

    // 유효기간 계산 메서드
    private LocalDate calcLocalDate(String dateStr, String termStr) {
        int year = Integer.parseInt(dateStr.substring(0, 4));
        int month = Integer.parseInt(dateStr.substring(5, 7));
        int day = Integer.parseInt(dateStr.substring(8, 10));
        int term = termMap.get(termStr);

        month += term;
        if (month > 12) {
            year += month / 12;
            month %= 12;
        }
        day -= 1;
        if (day < 1) {
            day = 28;
            month -= 1;
        }
        if(month < 1) {
            month = 12;
            year--;
        }

        return LocalDate.of(year, month, day);
    }
}

 

 

 

728x90
반응형

댓글