ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [프로그래머스] 개인정보 수집 유효기간 자바
    코딩테스트 2025. 3. 30. 17:37
    반응형

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

     

    프로그래머스

    SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

    programmers.co.kr

    문제

    오늘 날짜 today, 약관 종류별 개인정보 보관기간(개월) terms, 각 개인정보 별 수집일자와 약관종류를 담은 privacies를 바탕으로, 개인정보 보관기관을 경과하여 삭제가 필요한 privacy 번호를 int[] 에 담아 반환한다.

     

    제한사항 :

    1. 모든 월은 28일 씩 존재한다.

    2. 삭제가 필요한 privacy는 하나 이상 존재한다.

     

    생각

    우선 terms의 각 약관 종류별 기간으로 환산해 map에 담은 후,

    각 개인정보의 수집 날짜에 보관기간을 더한 값 < 오늘날짜이면 리스트에 추가하는 로직으로 작성하였음.

    public static int[] solution(String today, String[] terms, String[] privacies) {
            Map<String, Integer> termsMap = new HashMap<>();
            List<Integer> results = new ArrayList<>();
    
            for (String s : terms) {
                String[] term = s.split(" ");
                int period = Integer.parseInt(term[1]) * 28 ; //약관 개월수. 모든 달은 28일까지 존재.
                termsMap.put(term[0], period);
            }
    
            String[] now = today.split("\\.");
            for(int i = 0 ; i < privacies.length; i++){
                String[] pArr = privacies[i].split(" ");
                String catg = pArr[1];
    
                int cnt = 1;
                String[] pDate = pArr[0].split("\\.");
                int pY = Integer.parseInt(pDate[0]);
                int pM = Integer.parseInt(pDate[1]);
                int pD = Integer.parseInt(pDate[2]);
                int nowY = Integer.parseInt(now[0]);
                int nowM = Integer.parseInt(now[1]);
                int nowD = Integer.parseInt(now[2]);
    
                while(cnt < termsMap.get(pArr[1])){
                    pD ++;
                    if(pD > 28){
                        pM ++;
                        pD = 1;
                        if(pM > 12){
                            pY ++;
                            pM = 1;
                        }
                    }
                    cnt ++;
                }
    
                if(pY < nowY){
                    results.add(i +1);
                } else if(pY == nowY){
                    if(pM == nowM){
                        if(pD < nowD){
                            results.add(i +1);
                        }
                    }
                }
            }
    
            int[] answer = new int[results.size()];
            for(int i = 0; i < results.size(); i++){
                answer[i] = results.get(i);
            }
    
            return answer;
        }

     

    발견된 문제

    1.보관만료기간을 계산하는 과정이 비효율적이다.
    2. 날짜 비교 로직이 부정확하다. (같은 연도 다른 달의 경우 등)

     

    수정

    1.일수로 변환하는 메서드를 별도로 생성
    2. 날짜 비교 로직을 간략하고 명확하게 변경.

     

    public int[] solution(String today, String[] terms, String[] privacies) {
            Map<String, Integer> termsMap = new HashMap<>();
            List<Integer> results = new ArrayList<>();
    
            // 약관 기간을 일수로 변환 (모든 달은 28일 기준)
            for (String term : terms) {
                String[] parts = term.split(" ");
                termsMap.put(parts[0], Integer.parseInt(parts[1]) * 28);
            }
    
            // 오늘 날짜를 일수로 변환
            String[] todayParts = today.split("\\.");
            int todayDays = convertToDays(todayParts[0], todayParts[1], todayParts[2]);
    
            for (int i = 0; i < privacies.length; i++) {
                String[] privacy = privacies[i].split(" ");
                String[] dateParts = privacy[0].split("\\.");
                String category = privacy[1];
    
                // 수집 일자 + 유효기간 계산
                int expiryDays = convertToDays(dateParts[0], dateParts[1], dateParts[2]) + termsMap.get(category);
    
                // 만료 여부 확인
                if (expiryDays <= todayDays) {
                    results.add(i + 1);
                }
            }
    
            return results.stream().mapToInt(i->i).toArray();
        }
        // 날짜를 총 일수로 변환
        private int convertToDays(String year, String month, String day) {
            int y = Integer.parseInt(year);
            int m = Integer.parseInt(month);
            int d = Integer.parseInt(day);
            return (y * 12 * 28) + (m * 28) + d;
        }

     

    알게 된 점

    1. 날짜를 계산할 때는 연도까지 포함하여야 보다 정확하게 계산할 수 있다.
    2. List를 array로 변환할 때 list.stream().mapToType(i -> i).toArray(); 로 간단하게 변경가능하다.
    반응형

    댓글

Designed by Tistory.