본문 바로가기

코딩 테스트/프로그래머스

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

프로그래머스

2023 KAKAO BLIND RECRUITMENT

 

 

프로그래머스

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

programmers.co.kr

개인정보 수집 유효기간

 

문제 정리
1. 고객의 약관 동의를 얻어서 수집된 1~n번으로 분류되는 개인정보 n개가 있다.
2. 수집된 개인 정보는 유효기간 전까지만 보관 가능, 유효기간이 지나면 반드시 파기된다.
3. 모든 달은 28일까지 있다고 가정된다.
4. 매개변수 today는 YYYY.MM.DD 형태로 오늘 날짜를 나타낸다.
5. 배열 terms[] = {약간종류, 유효기간}
6. 배열 privacies[] = i+1번 개인정보의 수집 일자와 약관 종류를 나타낸다.

 

 

시작하기 전 접근 방법
1. 약관종류와 유효기간을 " "로 분리하여 map에 key,value 형태로 저장한다.
2. yyyy,mm,dd에 대한 날짜를 각각 비교하기 위해서 .을 기준으로 분리한다.
3. 년,월,일로 나눠져 있는 날을 계산하기 쉽게 일을 기준으로 계산한다.
4. 약관종류에 따라서 유효기간을 +하여 계산하고 계산한 유효기간이 범위에 넘어가는지 확인을 하여 체크한다.

 

테스트 입출력 예

 

 

1. 배열 terms에 있는 약관종류와 유효기간을 분리하여 hashMap에 저장한다.
배열 값은 String으로 되어 있기 떄문에 int형으로 변환한다.
HashMap<String,Integer>map = new HashMap<>();

for(String entity : terms){
	String[] parts = entity.split(" ");
	String key = parts[0];
	int value = Integer.parseInt(parts[1]);
	map.put(key,value);
}

 

2. 매개변수 today에 대해서 년,월,일로 분리한다.
int year = Integer.parseInt(today.split("\\.")[0]);
int month = Integer.parseInt(today.split("\\.")[1]);
int day = Integer.parseInt(today.split("\\.")[2]);

 

 

3. 약관종류와 개인정보 수집일자를 담고 있는 배열 privacies를 약관종류와 수집일자로 먼저 나눈다.
3-1. 이제 약관에 저장한 약관종류를 통해 유효기간 value값을 가져온다. value값은 mm이기 때문에 dd로 바꿔주기 위해서 문제에서 한달은 28일로 가정했기 때문에 value값*28을 한다.
String date = privacies[i].split(" ")[0];
int type = map.get(privacies[i].split(" ")[1])*28;

 

 

4. 이제 비교할 수집일자에 대한 날짜를 dd형태로 바꾼다!
4-1. 바꾸기 위해서는 today날짜와 비교를 하여 계산한다.
4-2. type(유효기간)이 유효기간 범위(checkDay)보다 더 커버리면 유효 기간 범위가 넘어가 버린다.
int checkDay = (year-Integer.parseInt(date.split("\\.")[0]))*28*12
        			+(month-Integer.parseInt(date.split("\\.")[1]))*28
        			+(day-Integer.parseInt(date.split("\\.")[2]));
        			
	if(type<=checkDay) {
	list.add(i+1);
		}

 

 

5. 값을 list에 저장한 이유는 정답에 해당하는 크기가 정해져 있지 않고 상화에 따라 크기가 변화하기 때문에 list에 저장하였다.
6. 마지막으로 list에 있는 값을 배열로 변환하기 위하여 stream을 사용한다.
list.stream().filter(i->i!=null).mapToInt(i->i).toArray();

 

 

 

최종 코드
더보기
import java.util.*;

class Solution {
    public int[] solution(String today, String[] terms, String[] privacies) {
        HashMap<String,Integer>map = new HashMap<>();
        ArrayList<Integer> list = new ArrayList<>();
        for(String entity : terms){
            String[] parts = entity.split(" ");
            String key = parts[0];
            int value = Integer.parseInt(parts[1]);
            map.put(key,value);
        }

        int year = Integer.parseInt(today.split("\\.")[0]);
        int month = Integer.parseInt(today.split("\\.")[1]);
        int day = Integer.parseInt(today.split("\\.")[2]);
        
        for(int i=0; i<privacies.length; i++) {
        	String date = privacies[i].split(" ")[0];
        	int type = map.get(privacies[i].split(" ")[1])*28;
        	
        	int checkDay = (year-Integer.parseInt(date.split("\\.")[0]))*28*12
        			+(month-Integer.parseInt(date.split("\\.")[1]))*28
        			+(day-Integer.parseInt(date.split("\\.")[2]));
        			
        	if(type<=checkDay) {
        		list.add(i+1);
        	}
        }
        
        return list.stream().filter(i->i!=null).mapToInt(i->i).toArray();
    }
    
}