빙응의 공부 블로그

[Programmers]Lv.1 개인정보 수집 유효기간 본문

Argorithm

[Programmers]Lv.1 개인정보 수집 유효기간

빙응이 2023. 12. 1. 17:32

 


📝풀이

정말 어려웠다.... 카카오

 

알아야 하는 것은 3가지이다.

1. String 처리 : terms, privacies를 처리하려면 split을 써야한다. 

2. 날짜 처리 방식 : 나는 자바의 LocalDate를 사용했는데, 날짜 형식 상관없이 모든 날짜를 Day 형식으로 풀어서 푸는 방법도 있다. 

3. Map: terms를 효율적으로 처리하기 위해서 Map 사용되는게 권장된다. 

 

 

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

public class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        LocalDate localToday = getLocalDate(today);

        HashMap<String, Integer> termsMap = new HashMap<>(); //terms를 key-value로 저장
        List<Integer> index = new ArrayList<>(); //만료한 날짜 인덱스 저장

        for (String term : terms) { //해시 맵 초기화
            String[] tempTerm = term.split(" ");
            termsMap.put(tempTerm[0], Integer.parseInt(tempTerm[1]));
        }

        for (int i = 0; i < privacies.length; i++) {
            String[] tempPrivacy = privacies[i].split(" ");
            LocalDate localPrivacy = getLocalDate(tempPrivacy[0]);

            if (localToday.isAfter(localPrivacy.plusMonths(termsMap.get(tempPrivacy[1])).minusDays(1))) {
                index.add(i + 1); 
            }
        }

        int[] answer = index.stream().mapToInt(Integer::intValue).toArray();
        return answer;
    }

    public LocalDate getLocalDate(String day) { //String 날짜를 LocalDate 형식으로 변환 
        String[] localDay = day.split("\\.");
        return LocalDate.of(Integer.parseInt(localDay[0]), Integer.parseInt(localDay[1]), Integer.parseInt(localDay[2]));
    }
}