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

바그다드·2023년 3월 23일
0

알고리즘

목록 보기
14/14

문제




리뷰

다른 조건들은 생각보다 구현하는게 쉬워서 빠르게 구현할 수 있었지만
테스트 케이스에서 실패가 뜨는 상황이 꽤 많았다.

풀이는 2가지가 있다. 
1. 년과 월을 모두 날짜로 환산하여 누적하는 방법,
2. 년, 월을 기준으로 나눠서 계산하는 방법

2번 풀이도 분명히 잘 계산한거 같은데 결과가 제대로 나오질 않아
1번 풀이로 바꿔서 계산했는데 정답이 나왔다,
그런데 아무리 생각해도 2번 풀이도 정답이 나와야 하는데 몇 개의 테스트 케이스에서 오답이 나와
확인하고 수정하였다.
그리고 드디어 2번 풀이도 성공하였다!!(시간을 얼마나 쏟은건지 모르겠다ㅜ)
드디어 잘 수 있다ㅜㅜ

풀이 1

public Integer[] solution(String today, String[] terms, String[] privacies) {
        Integer[] answer = {};
        List<Integer> list = new ArrayList<>();

        Map<String,Integer> map = new HashMap<>();
        for(int i = 0; i < terms.length; i++){
            String tmp = terms[i].split(" ")[0];
            Integer term = Integer.parseInt(terms[i].split(" ")[1]);
            map.put(tmp, term);
        }

        Integer toDayYear = Integer.parseInt(today.split("\\.")[0])*12*28;
        Integer toDayMonth = Integer.parseInt(today.split("\\.")[1])*28;
        Integer toDayDay = Integer.parseInt(today.split("\\.")[2]);

        Integer toDayTotal = toDayYear + toDayMonth + toDayDay;

        for(int y = 0; y < privacies.length; y++){
            String tmpEx = privacies[y].split(" ")[0];
            Integer years = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[0])*12*28;
            Integer months = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[1])*28;
            Integer days = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[2]);
            String ttmp = privacies[y].split(" ")[1];

            int term = map.get(ttmp)*28;

            Integer expireDay = years + months + days + term;
            if(toDayTotal >= expireDay){
                list.add(y+1);
            }
        }

        answer = list.toArray(new Integer[0]);


        return answer;
    }

풀이 2

public Integer[] solution(String today, String[] terms, String[] privacies) {
        Integer[] answer = {};
        List<Integer> list = new ArrayList<>();
        
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0; i < terms.length; i++){
            String tmp = terms[i].split(" ")[0];
            Integer term = Integer.parseInt(terms[i].split(" ")[1]);
            map.put(tmp, term);
        }
        
        for(int y = 0; y < privacies.length; y++){
            String tmpEx = privacies[y].split(" ")[0];
            Integer years = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[0]);
            Integer months = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[1]);
            Integer days = Integer.parseInt(privacies[y].split(" ")[0].split("\\.")[2]);
            String ttmp = privacies[y].split(" ")[1];
            
            int term = map.get(ttmp);
            
            if(term >= 12){
                years += (term/12);
                if((term%12) == 0){
                }else if((term%12)+months>12){
                    years+=1;
                    months = (term%12)+(months-12);  
                }else{
                    months += (term%12);
                }  
            }else if(term+months>12){
                years += 1;
                months = term + (months - 12);
            }else{
                months += term;
            }
            String tempToDay = today.replace(".","");
            Integer toDayTotal = Integer.parseInt(tempToDay);
            Integer expireDay = (years*10000) + (months*100) + days;
            System.out.println(expireDay-1);
            if(toDayTotal >= expireDay){
                list.add(y+1);
            }
        }
                
        answer = list.toArray(new Integer[0]);
        

        return answer;
    }
profile
꾸준히 하자!

0개의 댓글