[PGS] 개인정보 수집 유효기간 - JAVA

최영환·2023년 8월 27일
0

Programmers

목록 보기
27/43

💡 문제

💬 입출력 예시

📌 풀이(소스코드)

import java.util.*;


class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        int n = privacies.length;
        List<Integer> answer = new ArrayList<>();
        int lastDay = 28;	// 1달은 28일이 최대
        // 오늘 날짜를 연,월,일로 분리
        int nowYear = Integer.parseInt(today.substring(0,4));
        int nowMonth = Integer.parseInt(today.substring(5,7));
        int nowDay = Integer.parseInt(today.substring(8, 10));

		// 오늘 총 일 수
        int totalNow = (nowYear * lastDay * 12) + (nowMonth * lastDay) + nowDay;

        // terms 에 있는 약관 정보를 Map 에 저장
        Map<Character, Integer> termMap = new HashMap<>();
        for (String term: terms) {
            termMap.put(term.charAt(0), Integer.parseInt(term.substring(2)));
        }

		// 개인정보 리스트 순회
        for (int i = 0; i < n; i++) {
            String privacy = privacies[i];
           	// 개인정보를 약관, 가입 연,월,일으로 분리
            char termType = privacy.charAt(11);
            int year = Integer.parseInt(privacy.substring(0,4));
            int month = Integer.parseInt(privacy.substring(5,7));
            int day = Integer.parseInt(privacy.substring(8, 10));

			// 가입의 총 일 수
            int totalPrivacy = (year * lastDay * 12) + (month * lastDay) + day;
            // 유효기간 계산
            int term = termMap.get(termType) * lastDay;
            int validateDay = totalPrivacy + term;
            // 유효기간이 지났는지 확인
            if (validateDay <= totalNow) {
                answer.add(i + 1);
            }
        }

        return answer.stream().mapToInt(i -> i).toArray();

    }
}

📄 해설

접근

  • 2023 카카오 기출문제. 꼭 직접 풀어보길 바란다.
  • 문자열을 정수로 바꾸고, 오늘의 일 수와 개인정보 유효기간의 일 수를 계산하고, 비교하는 문제
  • 각각의 일자를 전체 일 수로 바꾼다는 아이디어가 핵심이다.
  • 날짜 핸들링 문제가 나왔다고 해서 DateLocalDate 같은 방식을 생각하면 안된다. 이 문제의 한달은 28일이 최대다.

과정

  • today 를 전체 일 수로 변환한다.
  • terms 에 있는 약관 정보를 HashMap 에 약관 종류를 key 값으로, 개월 수를 value 로 저장한다.
  • 개인정보 리스트인 privacies 를 순회하면서 각각의 개인정보의 유효기간이 만료되었는지를 확인한다.
    • 먼저 각각의 개인정보 별로 저장된 일자를 전체 일 수로 변환하고, 약관의 종류에 따라 만료일을 계산한다.
    • 만료 된 경우는 오늘의 총 일수 가 만료 일 수 보다 크면 만료가 된 경우이다.
      이 경우 현재의 인덱스 값 + 1 을 리스트에 담는다.
  • 리스트 순회가 끝났다면 정답 리스트를 배열로 변환하여 반환한다.
profile
조금 느릴게요~

0개의 댓글