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

카카오코딩테스트

목록 보기
1/1
post-thumbnail

<문제>

오늘 날짜, 약관종류와 유효기간, 개인정보수집일자와 약관 종류가 String 으로 주어지고
파기 대상이 되는 개인정보의 번호를 구하라.
https://school.programmers.co.kr/learn/courses/30/lessons/150370

<나의 풀이>

import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        ArrayList <Integer> list = new ArrayList<>();
        // 1. 오늘 날짜 변환
        int now = dateChanger(today);    
        // 2. 기준 날짜  map 으로 변환
        Map<String, Integer> map = new HashMap<>();
        for (String s : terms){
            map.put(s.substring(0,1), Integer.parseInt(s.substring(2))*28);
        }
        
        // 3. 파기 대상 정보 선별
        for (int i=0; i<privacies.length; i++){
            String key = privacies[i].substring(privacies[i].length()-1);
            int validate = map.get(key) + dateChanger(privacies[i]);
            if(validate <= now) list.add(i+1);
        } 
        
        return list.stream().mapToInt(integer -> integer).toArray();
    }
    
    
    public int dateChanger (String date){
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));
        int date = Integer.parseInt(date.substring(8,10));
        return year*12*28 + month*28 + date;
    }
    
}

<다른 사람의 풀이>


import java.util.*;

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;
    }
}

<핵심 개념>

날짜를 어떻게 바꿔줄 지가 핵심인 것 같다.

<피드백>

자잘한 메소드를 헷갈려한다.
입력할 때 헷갈리는 메소드들 정리해놓고 봐야겠다.

profile
두둥탁 뉴비등장

0개의 댓글