프로그래머스 (카카오) | LV.1 개인정보 수집 유효기간 - 단순 구현 문풀 (Java)

728x90

⬛ 프로그래머스 (카카오) | LV.1 개인정보 수집 유효기간 - 단순 구현 문풀 (Java)

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

 

프로그래머스

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

programmers.co.kr

문제 설명
제한 사항


💚문제 접근 방식

n개의 개인정보 중 약관 type에 따라 현재 시점에서 파기할 개인정보 번호 목록을 리턴하는 문제이다.

문제가 요구하는 흐름대로 코드에 구현하면 된다.

 

🎈 [주의] split 메서드 사용시 실수하기 쉬운 것 (.) 구분자 → split(”\\ .”)

계속 오류가 나서 원인을 못찾다가 split하는 부분이 (.)에 대해서는 인식을 못하는 게 문제였다.

사실 예전에도 이런 적이 있었는데 … split이 정상적으로 안될 경우 \\를 앞에 붙여주자.

특히 (.) 을 구분자로 자르는 경우가 꽤 될텐데 외워둬야 할 것 같다.

 

1) Info 클래스를 정의했다. (yy-mm-dd) 를 내부적으로 갖는 클래스이다.

 

2) Map으로 terms 순회하며 각 type별 연장 기간을 value로 담아뒀다.

 

3) today로 들어온 현재 시점의 날짜도 Info 형태도 변환해준다.

 

4) privacies를 순회하면서 각 type별 date에 대하여→ 유효 끝날짜를 갖는 Info 객체를 생성한다.

 

5) 동시에 targetInfo와 endInfo 비교하며 조건에 맞는 index를 answer에 담아주고 리턴했다.

 

💚 제출 코드

import java.util.*;

class Info {
    int yy, mm, dd;

    Info(int yy, int mm, int dd) {
        this.yy = yy;
        this.mm = mm;
        this.dd = dd;
    }
}

class Solution {
    static Map<String, Integer> map;

    // getInfo
    public static Info getInfo(String date) {
        String[] str = date.split("\\\\.");
        return new Info(Integer.parseInt(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]));
    }

    // type 별 Info 반환
    public static Info getEndInfo(Info info, String type) {
        int plusMon = map.get(type);

        int yy = info.yy;
        int mm = info.mm + plusMon;
        int dd = info.dd;

        while (mm > 12) {
            mm -= 12;
            yy++;
        }

        return new Info(yy, mm, dd);
    }

    // 솔루션 함수
    public List<Integer> solution(String today, String[] terms, String[] privacies) {
        map = new HashMap<>();
        for (String tr : terms) {
            String[] tmp = tr.split(" ");
            map.put(tmp[0], Integer.parseInt(tmp[1]));
        }

        Info targetInfo = getInfo(today);

        int idx = 0;
        List<Integer> answer = new ArrayList<>();

        for (String pv : privacies) {
            idx++;
            String[] tmp = pv.split(" ");

            String date = tmp[0];
            String type = tmp[1];

            Info info = getInfo(date);
            Info endInfo = getEndInfo(info, type);

            if (targetInfo.yy > endInfo.yy) {//작다면 add
                answer.add(idx);
            } else if (targetInfo.yy == endInfo.yy && targetInfo.mm > endInfo.mm) { //같은 년도이면서 mm 작으면 add
                answer.add(idx);
            } else if (targetInfo.yy == endInfo.yy && targetInfo.mm == endInfo.mm && targetInfo.dd >= endInfo.dd) {//같은 년도, 같은 달 이면서 dd까지 작거나 같아도 add 
                answer.add(idx);
            }
        }

        return answer;
    }
}

💚회고 & 시간복잡도

시간복잡도의 경우, 데이터 크기가 작아서 딱히 문제 될 건 없어보인다.

다만, split할 때 점(.) 하나로 구분해야 할 경우 \\를 붙여서 뽑아야 하는 걸 주의하자.

728x90