[2023 Kakao Blind] 개인정보 수집 유효기간 (Java)

허주환·2023년 2월 28일
0
post-thumbnail

2023 Kakao Blind - 개인정보 수집 유효기간
Level: 1
Language: Java
Comment: Hash Map, 단순 구현
URL: https://programmers.co.kr/learn/courses/30/lessons/150370
전체 코드: https://github.com/juhwanHeo/algorithm/blob/main/src/main/java/com/programmers/kakao2023/blind/Test1.java
테스트 코드: https://github.com/juhwanHeo/algorithm/blob/main/src/test/java/com/programmers/kakao2023/blind/Test1Test.java

0. 문제 설명

  • 문제 URL 참고
  • 약관별(terms)로 다른 등록된 개인정보 유효 기간(privacies)을 today(오늘 날짜)와 비교해서 파기해야할 약관 번호를 구하는 문제

1. 로직

I. 약관 별로 유효기간을 HashMap에 저장

for (String term : terms) {
	String[] split = term.split(" ");
	map.put(split[0], Integer.parseInt(split[1]));
}

II. 년/월/일을 일로 변환

  • 조건에 모든 달이 28일 까지 있음
private static int getDay(String date) {
    String[] split = date.split("\\.");
    int year = Integer.parseInt(split[0]) * 12 * 28;
    int month = Integer.parseInt(split[1]) * 28;
    int day = Integer.parseInt(split[2]);
    return year + month + day;
}

III. 파기해야할 약관 찾기

  • 오늘 날짜 - 개인정보 수집 일자(diff) 한 값이
    약관 타입으로 map에서 가져온 약관 유효 기간과 비교해서
    해당 약관 유효 기간이 가져온 값이 diff보다 더 작거나 같으면 정답에 추가
int todayValue = getDay(today);
int index = 1;
for (String privacy : privacies) {
    String[] split = privacy.split(" ");
    String date = split[0];
    String type = split[1];
    int termDay = getDay(date);
    int diff = (todayValue - termDay) / 28; // '월'로 변환
    if (map.get(type) <= diff) list.add(index);

    index++;
}

2. 전체 코드

/*
 * @2023 KAKAO BLIND RECRUITMENT
 * @TestName: 개인정보 수집 유효기간
 * @URL: https://programmers.co.kr/learn/courses/30/lessons/150370
 */
public class Test1 {

    public static int[] solution(String today, String[] terms, String[] privacies) {
        List<Integer> list = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();

        int todayValue = getMonth(today);
        for (String term : terms) {
            String[] split = term.split(" ");
            map.put(split[0], Integer.parseInt(split[1]));
        }

        int index = 1;
        for (String privacy : privacies) {
            String[] split = privacy.split(" ");
            String date = split[0];
            String type = split[1];
            int termDay = getMonth(date);
            int diff = (todayValue - termDay) / 28;
            if (map.get(type) <= diff) list.add(index);

            index++;
        }

        return list.stream()
                .sorted() // 없어도 됨
                .mapToInt(Integer::intValue)
                .toArray();
    }

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

후기

단순 구현 문제라 금방 품

profile
Junior BE Developer

0개의 댓글