Algorithm/프로그래머스

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

녱녱 2023. 7. 30.

목차

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

주어진 약관들 중에서 개인정보가 만료된 약관의 번호를 구하면 되는 문제이다. 

 

1. 누적된 총 날짜를 가져오는 함수 getTotalDate를 생성한다

2. 비교하기 쉽도록 주어진 약관들을 map을 생성해 넣어준다

3. 주어진 약관들을 비교하며 파기해야하는 약관인지 확인한다

 

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        List<Integer> answer = new ArrayList<>();        
        Map<String, Integer> period = new HashMap<>();
        
        int date = getTotalDate(today);
        
        for(String t: terms){
            String[] term = t.split(" ");     
            period.put(term[0], Integer.valueOf(term[1]));
        }
        
        for(int i = 0; i < privacies.length; i++){
            String[] privacy = privacies[i].split(" ");
            if(getTotalDate(privacy[0]) + (period.get(privacy[1]) * 28) <= date) {
                answer.add(i + 1);
            }
        }
        
        int[] answerArray = new int[answer.size()];
        for (int i = 0; i < answer.size(); i++) {
            answerArray[i] = answer.get(i);
        }

        return answerArray;
    }
    
    private int getTotalDate(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;
    }
}

사실 처음엔 이중for문을 사용할까 했는데 terms랑 privacies랑 비교하면서 코드가 너무 더러워지길래 terms를 map을 사용해 관리했다.  그리고 마지막 부분에 list를 array로 바꿔줄 때 새로 배열을 선언하고 for문 돌리기 귀찮아서 strem을 썼었는데 너무 오래걸려서 그냥 for문을 돌려서 구했다 생각보다 쉽게 풀어쓰용

 

 

 

 

 

 

'Algorithm > 프로그래머스' 카테고리의 다른 글

[Lv. 1] 체육복  (0) 2023.08.08
[Lv. 2] 영어 끝말잇기  (0) 2023.07.30
[Lv. 1] 햄버거 만들기  (0) 2023.07.29
[Level 1] 숫자 문자열과 영단어  (0) 2023.07.23
[고득점kit/완전탐색] 피로도  (0) 2023.07.16

댓글