학습/프로그래머스

[프로그래머스] 개인정보 수집 유효기간(java)

태기 2023. 1. 29. 22:16

문제

https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

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

programmers.co.kr


나의 풀이

import java.util.Arrays;
import java.util.HashMap;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        int priLen = privacies.length;
        int[] answer = new int[priLen];
        HashMap<String, Integer> map = new HashMap<>();

        int todays = getDays(today, 0);

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

        for (int i = 0; i < priLen; i++) {
            String[] privacy = privacies[i].split(" ");
            int endDays = getDays(privacy[0], map.get(privacy[1]));
            answer[i] = endDays <= todays ? i + 1 : 0;
        }
        return Arrays.stream(answer).filter(num -> num > 0).toArray();
    }

    private int getDays(String date, int term) {
        String[] today = date.split("\\.");
        int year = Integer.parseInt(today[0]);
        int month = Integer.parseInt(today[1]);
        int day = Integer.parseInt(today[2]);

        return ((year * 12) + month + term) * 28 + day;
    }
}

약관 종류를 담는 HashMap 객체를 생성하여 terms 길이만큼 순회하여 map에 담는다. 이 때 terms의 길이 n 만큼 순회하므로 시간 복잡도는 O(n)이고 선형이다.

 

또 privacies 길이만큼 순회하여 약관의 마감 날짜를 계산한다. 이 때 privacies의 길이 n 만큼 순회하므로 시간 복잡도는 O(n)이고 선형이다.

 

getDays 메서드는 날짜와 기간을 인자로 받아서 일 수로 계산하여 반환한다.

 

따라서, 이 풀이는 2O(n)의 시간 복잡도를 갖게 되고 알고리즘 분석으로 증가 차수는 선형이 된다.