본문 바로가기

알고리즘 문제풀이/프로그래머스 Level 1

[2023 KAKAO BLIND RECRUITMENT/Java] 개인정보 수집 유효기간 ★

# 문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

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

programmers.co.kr

 

목차

 

카카오 문제 해설    2023 카카오 신입 공채 1차 온라인 코딩 테스트 for Tech developers 문제해설 링크
더보기

각 개인 정보가 수집된 날과 약관 종류로부터 보관 가능 날짜를 구하고,

오늘 날짜가 보관 가능 날짜를 지났는지를 구하면 되는 문제입니다.

개인 정보의 유효기간은 해시 테이블 등의 방법으로 구할 수 있습니다.

 

날짜를 비교할 때 YYYY.MM.DD 형태 그대로 비교해도 되지만,

2000년 1월 1일로부터 며칠이 흘렀는지를 계산하면 정수 형태로 쉽게 비교할 수 있습니다.

 

프로그래머스 다른사람 풀이 참고 1 (홍희표 , devsigner9920 , 최강현 , 윤창현 / 링크)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        List<Integer> answer = new ArrayList<>();
        Map<String, Integer> termMap = new HashMap<>();
        int date = getDate(today);

        for (String s : terms) {
            String[] term = s.split(" ");

            termMap.put(term[0], Integer.parseInt(term[1]));
        }
        for (int i = 0; i < privacies.length; i++) {
            String[] privacy = privacies[i].split(" ");

            if (getDate(privacy[0]) + (termMap.get(privacy[1]) * 28) <= date) {
                answer.add(i + 1);
            }
        }
        return answer.stream().mapToInt(integer -> integer).toArray();
    }

    private int getDate(String today) {
        String[] date = today.split("\\.");
        int year = Integer.parseInt(date[0]);
        int month = Integer.parseInt(date[1]);
        int day = Integer.parseInt(date[2]);
        return (year * 12 * 28) + (month * 28) + day;
    }
}

날짜를 정수로 계산하는 방법 기억해두자 +ㅇ+ !!! 메모메모~ 

 

프로그래머스 다른사람 풀이 참고 2 (LocalDate 클래스 사용) (junee613 / 링크)
import java.time.*;
import java.time.format.*;
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        List answer = new ArrayList();

        DateTimeFormatter formatter =  DateTimeFormatter.ofPattern("yyyy.MM.dd");
        LocalDate date = LocalDate.parse(today, formatter);

        // 찾기 쉽도록 termsMap 구성
        Map<String, Integer> termsMap = new HashMap<>();

        for(int i=0; i<terms.length; i++) {
            String[] term = terms[i].split(" ");
            termsMap.put(term[0], Integer.valueOf(term[1]));
        }

        for(int i=0; i<privacies.length; i++) {
            String[] privacy = privacies[i].split(" ");
            LocalDate privacyRegisterYmdt = LocalDate.parse(privacy[0], formatter).plusMonths(termsMap.get(privacy[1])).minusDays(1);


            if (privacyRegisterYmdt.isBefore(date)) {
                answer.add(i+1);
            }
        }

        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}