본문 바로가기
공부/2024 항해99코딩클럽

99클럽 3기 코테 스터디 26일차 TIL /[프로그래머스] 개인정보 수집 유효기간 자바 풀이 (2023 KAKAO BLIND RECRUITMENT )

by 푸딩코딩 2024. 8. 16.
728x90
반응형

 

 

 

 

 

 

1. 오늘의 학습 문제 


문제


 

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

 

코드


import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {

        ArrayList<Integer> answer=new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();


        for (String term : terms) { //약관 종류: 유효기간(int) 맵 생성, 찾는 복잡도 O(1)
            String[] arr = term.split(" ");
            map.put(arr[0], Integer.parseInt(arr[1]));
        }

        int cnt=1;
        int todayInt = Integer.parseInt(today.replace(".", ""));
        for (String privacy : privacies) {

            String[] arr = privacy.split(" "); //공백기준 수집일자: 약관 종류 나눔
            int plus = map.get(arr[1]); //더할 개월 수 저장
            int year= Integer.parseInt(arr[0].substring(0,4));
            int month = Integer.parseInt(arr[0].substring(5, 7));
            int date= Integer.parseInt(arr[0].substring(8));


            String f_date= date==1 ? "28": String.valueOf(date-1);
            if(f_date.equals("28")){
                plus-=1;
            }
            String f_month = ( plus + month <= 12) ? String.valueOf(plus + month) :
                    ( (plus + month) % 12 ==0)? "12" :String.valueOf((plus + month) % 12);
            if(f_month.length()==1){
                f_month = "0" + f_month;
            }
            if(f_date.length()==1){
                f_date = "0" + f_date;
            }
            String f_year= (plus + month <= 12) ? String.valueOf(year):
                    ( (plus + month) % 12 ==0)? String.valueOf(year+ (plus + month) / 12 -1): String.valueOf(year+ (plus + month) / 12);

            int endDate= Integer.parseInt(f_year+f_month+f_date);

            if(todayInt > endDate ){
                answer.add(cnt);
            }
            cnt++;

            System.out.println(todayInt + " " + endDate);

        }

        int[] ans = new int[answer.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = answer.get(i);
        }

        return ans;
    }
}

 

내가 작성한 코드, 날짜-달-년도를 거슬러 올라가 각각 계산하여 yyyymmdd형태로 합치고, today와 비교한다. 

 

 

import java.util.ArrayList;
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> map = new HashMap<>();

        // 오늘 날짜를 계산하여 변수에 저장
        int checkDate = getDate(today);

        // 날짜를 계산하기 위해 약관 종류와 기간을 맵에 저장
        for (String s : terms) {
            String[] term = s.split(" ");
            map.put(term[0], Integer.parseInt(term[1]));
        }

        // 개인정보 배열 privacies를 순회하며 처리 완료된 개인정보의 인덱스를 찾아서 리스트에 추가
        for (int i = 0; i < privacies.length; i++) {
            String[] privacy = privacies[i].split(" ");

            // 처리 완료일 계산(개인정보 처리 시작일 + 처리 기간 * 28일)
            if (getDate(privacy[0]) + (map.get(privacy[1]) * 28) <= checkDate) {
                answer.add(i + 1);
            }
        }

        // 리스트를 배열로 변환하여 반환
        return answer.stream().mapToInt(i -> i).toArray();
    }

    // 날짜를 입력받아 계산하여 정수값으로 반환하는 메서드
    public static int getDate(String date) {
        String[] arr = date.split("\\.");

        int year = Integer.parseInt(arr[0]);
        int month = Integer.parseInt(arr[1]);
        int day = Integer.parseInt(arr[2]);

        // 날짜를 연도 * 12 * 28 + 월 * 28 + 일 로 계산하여 반환
        return (year * 12 * 28) + (month * 28) + day;
    }
}
출처: https://ittrue.tistory.com/492 [IT is True:티스토리]

 

더욱 간단한 풀이 

 

#99클럽 #코딩테스트 준비 #개발자 취업 #항해99 #TIL

 

 

728x90
반응형