프로그래머스_개인정보 수집 유효기간

LeeYulhee·2023년 8월 23일
0

💻 문제 출처 : 프로그래머스_개인정보 수집 유효기간

👉 내가 작성한 답


import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        
        Map<Character, Integer> termsMap = new HashMap<>();
        
        List<Integer> tempAnswer = new LinkedList<>();
        
        int todayYear = Integer.parseInt(today.substring(0, 4));
        int todayMonth = Integer.parseInt(today.substring(5, 7));
        int todayDay = Integer.parseInt(today.substring(8, 10));
        
        for(String s : terms) {
            termsMap.put(s.charAt(0), Integer.parseInt(s.substring(2, s.length())));
        }
        
        for(int i = 0; i < privacies.length; i++) {
            
            int changeMonth = termsMap.get(privacies[i].charAt(11));
            
            int privacyYear = Integer.parseInt(privacies[i].substring(0, 4));
            int privacyMonth = Integer.parseInt(privacies[i].substring(5, 7));
            int privacyDay = Integer.parseInt(privacies[i].substring(8, 10));
            
            privacyMonth = privacyMonth + changeMonth;
            
            if(privacyDay == 1) {
                privacyDay = 28;
                privacyMonth--;
            } else {
                privacyDay = privacyDay - 1;
            }
            
            if(privacyMonth > 12) {
                if(privacyMonth % 12 == 0) {
                    privacyYear += privacyMonth / 12 - 1;
                    privacyMonth = 12;
                } else {
                    privacyYear += privacyMonth / 12;
		                privacyMonth = privacyMonth % 12;
                }
            }
            
            if(todayYear > privacyYear) {
                tempAnswer.add(i + 1);
                continue;
            }
            if(todayYear == privacyYear && todayMonth > privacyMonth) {
                tempAnswer.add(i + 1);
                continue;
            }
            if(todayYear == privacyYear && todayMonth == privacyMonth && todayDay > privacyDay) {
                tempAnswer.add(i + 1);
                continue;
            }
        }
        
        int[] answer = new int[tempAnswer.size()];
        
        for(int i = 0; i < tempAnswer.size(); i++) {
            answer[i] = tempAnswer.get(i);
        }
        
        return answer;
    }
}

📌 문제 풀이 설명

  • Character를 키로, Integer를 value로 갖는 Map을 생성
  • Integer 값을 저장하는 List를 생성
  • today에서 연도 부분을 int로 변환해 todayYear에, 월 부분을 int로 변환해 todayMonth에, 일 부분을 int로 변환해 todayDay에 대입
  • 향상된 for문으로 terms 배열 순회
    • Map에 terms 요소의 첫 글자(약관 종류)를 키로, 2번 인덱스부터 마지막 인덱스까지의 값을 int로 변환해 value로 put
  • for문으로 0부터 privacies의 길이 미만까지 1씩 증가하며 순회
    • int 변수 changeMonth에 privacies 요소의 11번 째 인덱스(약관 종류)를 키로 갖고 Map의 value를 대입
    • privacies 요소에서 연도 부분을 int로 변환해 privacyYear에, 월 부분을 int로 변환해 privacyMonth에, 일 부분을 int로 변환해 privacyDay에 대입
    • privacyMonth에 privacyMonth와 changeMonth의 값을 더해 대입
    • 만약 privacyDay가 1과 같다면
      • privacyDay에 28을 대입하고 privacyMonth를 1 감소
        • 보관할 수 있는 날짜는 유효 기간을 더한 날의 - 1일이라서 감소
          • 다만 수집 일자가 1일이면 - 1을 했을 때 전 월 28일이 되어야 함
        • 파기 날짜가 개인정보 수집 일자 + 유효기간
      • 아니라면 privacyDay에 privacyDay - 1을 대입
    • 만약 privacyMonth가 12보다 크다면
      • 만약 privacyMonth를 12로 나눈 나머지가 0이면
        • privacyYear에 privacyMonth를 12로 나누고 - 1한 값을 더해서 대입
          • 나머지가 0이면 해가 바뀌지 않고 12월이기 때문
        • privacyMonth에 12 대입
      • 그게 아니라면
        • privacyYear에 privacyMonth를 12로 나눈 몫을 더해서 대입
        • privacyMonth에 privacyMonth에서 12를 나눈 나머지 대입
    • 만약 todayYear가 privacyYear보다 크다면
      • List에 i + 1을 추가하고 continue로 for문의 다음 순회로 넘어감
        • 현재 연도가 더 크면 파기 기간이 지났기 때문
    • 만약 todayYear가 privacyYear와 같고 todayMonth가 privacyMonth보다 크다면
      • List에 i + 1을 추가하고 continue로 for문의 다음 순회로 넘어감
        • 현재 연도가 같은데 현재 월이 더 크면 파기 기간이 지났기 때문
    • 만약 todayYear가 privacyYear와 같고 todayMonth가 privacyMonth가 같고 todayDay가 privacyDay 보다 크다면
      • List에 i + 1을 추가하고 continue로 for문의 다음 순회로 넘어감
        • 현재 연도와 현재 월이 같은데 현재 일자가 더 크면 파기 기간이 지났기 때문
  • int 배열 answer를 List의 크기로 생성
  • for문으로 0부터 List의 크기 미만까지 1씩 증가하며 순회
    • answer[i]에 List의 i 번째 요소 대입
  • for문 종료 후 return answer



👉 다른 사람이 작성한 답


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

📌 문제 풀이 설명

  • Integer 값을 저장하는 List를 생성
  • String을 키로, Integer를 value로 갖는 Map을 생성
  • int date 변수에 getDate에 today를 넣어 return된 값을 대입
    • getDate 메서드
      • String이 매개변수로 들어오면 split을 이용해 “.”을 기준으로 나눠 String 배열에 저장
      • int 변수 year에 date[0]을 int로 변환해서 대입, month와 day에도 date[1]과 date[2]를 int로 변환해 대입
      • year 12 28(연도를 일로 변환) + month * 28(월을 일로 변환) + day 값을 return
  • 향상된 for문으로 terms 순회
    • String 배열 term에 s를 “ “로 나눈 값을 저장
    • Map에 term의 0번째 요소를 key로, term의 1번째 요소를 int로 변환해 value로 put
  • for문으로 0부터 privacies의 길이 미만까지 1씩 증가하며 순회
    • String 배열 privacy에 privacy[i]를 “ “로 나눈 값을 저장
    • 만약 privacy[0]을 넣어 getDate를 실행한 값(개인정보 수집 일자 → 일로 변환) + Map에서 privacy[1]이 key일 때 해당하는 value * 28(유효 개월 → 일로 변환)한 값이 date보다 작거나 같다면
      • List에 i + 1 추가
  • for문 종료 후 answer를 stream으로 변환 → int로 변환 → 배열로 변환해서 return
profile
공부 중인 신입 백엔드 개발자입니다

0개의 댓글