[프로그래머스] 개인정보 수집 유효기간 - Java

코린이·2023년 6월 8일
0

프로그래머스

목록 보기
21/22

📢 [2023 KAKAO BLIND RECRUITMENT]개인정보 수집 유효기간

프로그래머스 문제 링크

풀이

사용언어 : Java

  1. 유효기간을 담은 1차원 문자열 배열 terms를 HashMap 형태로 <약간 종류 , 유효기간 > 저장한다.
  2. 수집된 개인정보 정보를 담은 privacies를 for문으로 돌며 개인정보 파기날짜를 계산한다.
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        
        // 개인정보 n개 약관마다 유효기간이 정해져있음 유효기간이 지나면 반드시 파기하기.
        // 모든달은 28일까지 있다고 가정
        
        // today : yyyy.mm.dd 형태
        // terms : 약관종류 유효기간(m)
        // 출력 : 파기해야할 개인정보의 번호를 오름차순 출력
        // 특수문자는 앞에 \\ or []로 감싸주기
        String[] tTemp = today.split("[.]");
        int tYear = Integer.parseInt(tTemp[0]);
        int tMonth = Integer.parseInt(tTemp[1]);
        int tDay = Integer.parseInt(tTemp[2]);
        
        // terms 값들을 hashMap에 저장
        Map<String, Integer> term = new HashMap<>();
        for(String s : terms) {
            String[] temp = s.split(" ");
            term.put(temp[0], Integer.parseInt(temp[1]));
        }
        
        int count = 0;
        boolean[] find = new boolean[privacies.length+1];
        
        
        int num = 1;
        for(String s : privacies){
            String[] temp = s.split(" ");
            String[] temp2 = temp[0].split("[.]");
            
            // 파기 날짜
            int year = Integer.parseInt(temp2[0]);
            int month = Integer.parseInt(temp2[1]);
            int day = Integer.parseInt(temp2[2]);
            
            month = month + term.get(temp[1]);
            if(month > 12) {
                int t = month / 12;
                int tt = month % 12;
                if(tt == 0) {
                    year += t-1;
                    month = 12;
                }
                else {
                    year += t;
                    month = tt;
                }

            }

            if(year < tYear || (year == tYear && month < tMonth ) || (year == tYear && month == tMonth && day <= tDay )){
                count+=1;
                find[num] = true;
            }
            num++;
        }
        
        
        int[] answer = new int[count];
        int idx = 0;
        for(int i = 1; i <= privacies.length; i++){
            if(find[i]) {
                answer[idx] = i;
                idx++;
            }
        }
        return answer;
    }
}

⚡ 구현할 때 발생한 실수

  • 특수문자로 split을 할때 특수문자를 []로 감싸주거나 앞에 \\을 붙여야 합니다.
    ex) split("\.") , split("[.]")
  • 유효기간이 최대1년이 안넘긴다고 착각하여 파기날짜를 계산할때 month가 12월이 넘어가면 +1만 해주었다.
    => month / 12 몫 만큼 year 더해주기.
  • month가 12의 배수일 경우 예외처리를 안해주었다.
    -> month % 12가 0일 경우 month = 12를 year += (month / 12) -1;를 해주었

✔️ check

정답을 boolean배열과 count로 answer를 저장했는데,
ArrayList --> 배열 방법을 사용해 코드 간략화 하기.

 List<Integer> answer = new ArrayList<>();
 String[] array = answer.toArray(new String[arrayList.size()]);
profile
초보 개발자

0개의 댓글