코딩 관련/코딩문제풀기

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

메리짱123 2024. 10. 21. 13:59
반응형

새로운것

result
    .stream()    //스트림의 각 요소를 주어진 함수를 통해 새로운 스트림으로 반환
    .mapToInt(Integer::intValue)	//객체 스트림을 int 스트림으로 변환
    .toArray(); 		//스트림을 배열로

 

import java.util.*;
import java.util.stream.*;

//비교 시에 Date 라이브러리 쓰면 안 됨(한 달 28일로 가정하므로)
//계약일시 + 유효기간 합에서 단순히 12를 나눠서 계산하면 12월인경우 곤란해짐
class Solution {
    int todayDay; 
    int todayMonth;
    int todayYear;
    public int[] solution(String today, String[] terms, String[] privacies) {
        this.todayDay = Integer.parseInt(today.split("\\.")[2]);
        this.todayMonth = Integer.parseInt(today.split("\\.")[1]);
        this.todayYear = Integer.parseInt(today.split("\\.")[0]);
        
        Map<String,Integer> termsMap = new HashMap<>();
        for(String term : terms){
            termsMap.put(term.split(" ")[0],Integer.parseInt(term.split(" ")[1]));
        }
        
        List<Integer> result = new ArrayList<>();
        for(int i=0;i<privacies.length;i++){
            String start = privacies[i].split(" ")[0];
            int duration = termsMap.get(privacies[i].split(" ")[1]);
            if(!isOK(start,duration)){
                result.add(i+1);
            }
        }
        
        return result.stream().mapToInt(Integer::intValue).toArray();
    }
    
    //모든 달은 28일로 가정
    public boolean isOK(String start, int duration){
        int dueDay = Integer.parseInt(start.split("\\.")[2]);
        int dueMonth = Integer.parseInt(start.split("\\.")[1]) + duration;
        int dueYear = Integer.parseInt(start.split("\\.")[0]);        

        while(dueMonth > 12){
            dueMonth -= 12;
            dueYear += 1;
        }
        
        
        if(dueDay == 1){
            dueDay = 28;
            if(dueMonth == 1){
                dueMonth = 12;
                dueYear -= 1;
            }else{
                dueMonth -= 1;
            }
        }else{
            dueDay -= 1;
        }
        
        
        
        
        if(dueYear < todayYear){
            return false;
        }else if(dueYear > todayYear){
            return true;
        }else{
            if(dueMonth < todayMonth){
                return false;
            }else if(dueMonth > todayMonth){
                return true;
            }else{
                if(dueDay < todayDay){
                    return false;
                }else{
                    return true;
                }
            }
        }
    
        
    }
}
반응형