말하는 컴공감자의 텃밭

프로그래머스 개인정보 수집 유효기간 <4점> - 자바(java) ArrayList 본문

알고리즘/Programmers - Java

프로그래머스 개인정보 수집 유효기간 <4점> - 자바(java) ArrayList

현콩 2023. 5. 20. 14:50
728x90

프로그래머스 개인정보 수집 유효기간
짜잘한 오류로 오래걸린 문제

 

한동안 짜잘하게 할게있어서 문제를 못풀었지모야요..

 

먼저 문제를 읽어보면 today, terms, privacies가 주어집니다.

세 정보를 받아서 개인정보 보호기간이 넘어간 자료를 출력하는 문제입니다.

terms"A 6", "B 12" 이런식으로 정보가 있는데 알파뱃은 코드입니다.

privacies날짜코드가 적혀있어서 코드에 해당하는 기간동안 정보를 보존합니다.

 

결국 현재시간과, 보호기간 + 개인정보 날짜를 비교해서 결과물로 찍으면 되는 문제입니다.

형변환과 split, 기초 반복문을 통해서 받아온 정보를 정리하고, 날짜 일수로만 계산해서 풀어봤습니다.

 

import java.util.ArrayList;

class Solution {
    public ArrayList<Integer> solution(String today, String[] terms, String[] privacies) {
        ArrayList<Integer> answer = new ArrayList<>(); // 하나하나 추가해주려고 ArrayList 사용
        String[] date = today.split("\\.| ");                          // ("\\.| ") 로 .과 공백 구분
        int YYYY = Integer.parseInt(date[0]);
        int MM = Integer.parseInt(date[1]);
        int DD = Integer.parseInt(date[2]);
        int to = ((YYYY - 2000) * 12 + MM) * 28 + DD;    // 정리한 날짜를 "일수"로 정리 -2000은 그냥 숫자 커질까봐 ㅎㅎ

        String[][] ter = new String[terms.length][2];          //terms 2차원 배열로 [[A,6],[B,12]] 이런식으로 사용하려구 선언

        for (int j = 0; j < terms.length; j++) {                     // ter에 값 넣기
            String[] pair = terms[j].split(" ");
            ter[j] = pair;
        }

        for (int i = 0; i < privacies.length; i++) {               // privacies 자료 정리
            String[] pri = privacies[i].split("\\.| ");
            int YYYY1 = Integer.parseInt(pri[0]);
            int MM1 = Integer.parseInt(pri[1]);
            int DD1 = Integer.parseInt(pri[2]);
            String alpha = pri[3];
            int CG = ((YYYY1 - 2000) * 12 + MM1) * 28 + DD1;  // privacies 날짜 => CG

            for (int k = 0; k < ter.length; k++) {
                if (ter[k][0].trim().equals(alpha.trim())) {       // 만약 alpha와 이차원배열의 코드가 같다면 
                    CG += (Integer.parseInt(ter[k][1]) * 28);  // 해당하는 벨류인 ter[k][1] 에 *28 해서 CG에 값 넣기.
                    break;
                }
            }

            if (to >= CG) {                                                   // 기간이 넘은 경우 
                answer.add(i + 1);
            }
        }

        return answer;
    }
}

 

 키, 벨류값을 위해서는 ter 이차원 배열보다 HashMap를 쓸걸 그랬네요

 

값 출력 오류때문에 시간 좀 걸렸는데 +4점이라니.. 도로록

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.ArrayList;
 
class Solution {
    public ArrayList<Integer> solution(String today, String[] terms, String[] privacies) {
        ArrayList<Integer> answer = new ArrayList<>();
        String[] date = today.split("\\.| ");
        int YYYY = Integer.parseInt(date[0]);
        int MM = Integer.parseInt(date[1]);
        int DD = Integer.parseInt(date[2]);
        int to = ((YYYY - 2000* 12 + MM) * 28 + DD;
 
        String[][] ter = new String[terms.length][2];
 
        for (int j = 0; j < terms.length; j++) {
            String[] pair = terms[j].split(" ");
            ter[j] = pair;
        }
 
        for (int i = 0; i < privacies.length; i++) {
            String[] pri = privacies[i].split("\\.| ");
            int YYYY1 = Integer.parseInt(pri[0]);
            int MM1 = Integer.parseInt(pri[1]);
            int DD1 = Integer.parseInt(pri[2]);
            String alpha = pri[3];
            int CG = ((YYYY1 - 2000* 12 + MM1) * 28 + DD1;
 
            for (int k = 0; k < ter.length; k++) {
                if (ter[k][0].trim().equals(alpha.trim())) {
                    CG += (Integer.parseInt(ter[k][1]) * 28);
                    break;
                }
            }
 
            if (to >= CG) {
                answer.add(i + 1);
            }
        }
 
        return answer;
    }
}
 
cs

 

728x90
Comments