본문 바로가기
JAVA/프로그래머스

[Lv.1] 프로그래머스 - 개인정보 수집 유효기간 : Java

by ♡˖GYURI˖♡ 2024. 5. 13.
 

프로그래머스

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

programmers.co.kr

 

 

 

이해하기

처음 접근했던 방식은 다음과 같다.

  • List에 저장해두고 int 배열로 변환해서 반환해야지
  • today를 기준으로 terms의 개월수만큼 뺀 날짜를 계산해서 따로 String 배열에 저장해야지
    • e.g. today = "2022.05.19", terms = {"A 6", "B 12", "C 3"}
    • expired[] = {"2021.11.18", "2021.05.18", "2022.02.18"}

여기까지 구현했는데 문제가 생겼다.

terms의 "A", "B", "C"를 구분해서 비교해줘야하는데, 나는 그냥 String 배열에 순서대로 넣어버려서 구별할 수 없게 된 것이다. ABC 순서라면 어떻게든 하겠는데, 두 번째 예시에서 나온 ZD는 사전순도 아니고... 어떻게 구별해야 할지 고민하다가나중에 Map을 추가했는데, 그렇다고 해도 이미 구현해놓은 코드로는 풀이할 수 없었다.

 

그래서 참고한 블로그⬇️

 

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

프로그래머스 programmers Level1 개인정보 수집 유효기간 - java 자바 ■ 문제 2023 KAKAO BLIND RECRUITMENT https://school.programmers.co.kr/learn/courses/30/lessons/150370 프로그래머스 코드 중심의 개발자 채용. 스택 기

gymdev.tistory.com

  • 일단 Map에 약관의 종류와 유효기간을 저장해둔다.
  • 오늘 날짜를 총 날짜수로 변환해준다.
    • 년도 * 12(개월) * 28(날짜) + 월 * 28 + 날짜
  • privacies 배열을 하나씩 확인해가며 약관이 유효한지, 또는 만료되었는지 확인한다.
    • privacies에서날짜와 약관 종류를 분리한다.
    • 약관 종류를 통해 Map에서 유효기간을 찾아온다.
    • 날짜의 달에 유효기간의 달 수를 더해준다.
    • 유효기간 경과 후 총 날짜 수를 구한다.
      • 년도 * 12(개월) * 28(날짜) + (월 + 유효기간의 달 수) * 28 + 날짜 - 1 (기간이라 - 1)
    • 계산된 전체 날짜 수를비교하여 폐기대상인지 확인  

 

문제풀이

import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        List<Integer> result = new ArrayList<>();        
        Map<String, String> map = new HashMap<>();
        
        for (String term : terms) {
            String[] termSplit = term.split(" ");
            map.put(termSplit[0], termSplit[1]);
        }
        
        int num = 1;
        Integer todayTotalDate = getTotalDate(today, 0);
        
        for (String p : privacies) {
            String[] privateSplit = p.split(" ");
            String privateDate = privateSplit[0];
            String privateTerm = privateSplit[1];
            Integer termsMonth = Integer.valueOf(map.get(privateTerm));
            
            Integer privateTotalDate = getTotalDate(privateDate, termsMonth) - 1;
            
            if (privateTotalDate < todayTotalDate) {
                result.add(num);
            }
            
            num++;
        }
        
        return result.stream().mapToInt(n -> n).toArray();
    }
    
    public Integer getTotalDate(String strDate, Integer termsMonth) {
        String[] dateSplit = strDate.split("\\.");
        Integer year = Integer.valueOf(dateSplit[0]);
        Integer month = Integer.valueOf(dateSplit[1]) + termsMonth;
        Integer day = Integer.valueOf(dateSplit[2]);
        
        return (year * 12 * 28) + (month * 28) + day;
    }
}

 

        Map<String, String> map = new HashMap<>();
        
        for (String term : terms) {
            String[] termSplit = term.split(" ");
            map.put(termSplit[0], termSplit[1]);
        }

 

Map에 약관 종류와 유효 기간 저장

 

        int num = 1;
        Integer todayTotalDate = getTotalDate(today, 0);

 

인덱스 번호를 나타낼 num과 오늘 날짜의 총 날짜수 구하기

파라미터로 날짜와 termMonth를 넘겨주는데, 오늘 날짜를 구하는 것이니 따로 termMonth는 필요하지 않기에 0을 넣어줌

 

    public Integer getTotalDate(String strDate, Integer termsMonth) {
        String[] dateSplit = strDate.split("\\.");
        Integer year = Integer.valueOf(dateSplit[0]);
        Integer month = Integer.valueOf(dateSplit[1]) + termsMonth;
        Integer day = Integer.valueOf(dateSplit[2]);
        
        return (year * 12 * 28) + (month * 28) + day;
    }

 

총 날짜수를 구하는 함수

 

        for (String p : privacies) {
            String[] privateSplit = p.split(" ");
            String privateDate = privateSplit[0];
            String privateTerm = privateSplit[1];
            Integer termsMonth = Integer.valueOf(map.get(privateTerm));
            
            Integer privateTotalDate = getTotalDate(privateDate, termsMonth) - 1;
            
            if (privateTotalDate < todayTotalDate) {
                result.add(num);
            }
            
            num++;
        }

 

privacies 배열을 순회하며 날짜와 약관 종류를 split을 통해 구하고, 약관 종류를 통해 유효 기간을 map에서 조회한다.

 

개인정보 수집 일자와 유효기간을 통해 총 날짜수를 구하고 기간이니 -1 해준다.

 

만약 개인 정보 수집 일자 + 유효기간이 오늘보다 이전에 끝났다면 result에 num을 add해준다.