반응형
🔺 문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
🔺 코드
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
43
44
|
import java.util.*;
class Solution {
static Map<String, String> map = new HashMap<>();
static List<Integer> list = new ArrayList<>();
public int[] solution(String today, String[] terms, String[] privacies) {
// 문자와 그에 따른 기간 넣기
for(String term : terms) {
String[] str = term.split(" ");
map.put(str[0], str[1]);
}
int YY = Integer.parseInt(today.split("\\.")[0]);
int MM = Integer.parseInt(today.split("\\.")[1]);
int DD = Integer.parseInt(today.split("\\.")[2]);
// 개인정보 수집일자
for(int i = 0 ; i < privacies.length ; i++) {
String[] privacy = privacies[i].split(" ");
String[] numbers = privacy[0].split("\\.");
int type = Integer.parseInt(map.get(privacy[1])) * 28; // 개인 정보 만료일
int diff = (YY - Integer.parseInt(numbers[0])) * 28 * 12
+ (MM - Integer.parseInt(numbers[1])) * 28
+ (DD - Integer.parseInt(numbers[2]));
if(diff >= type) {
list.add(i + 1);
}
}
// 출력하기
Collections.sort(list);
int[] answer = new int[list.size()]; // 파기해야 할 개인정보 번호
for(int i = 0 ; i < answer.length ; i++) {
answer[i] = list.get(i);
}
return answer;
}
}
|
cs |
🧩 해결 아이디어
• 문자열
- HashMap에 약관에 따른 보관 기간을 저장한다.
for(String term : terms) {
String[] str = term.split(" ");
map.put(str[0], str[1]);
}
- 기준일과 개인정보 수집일자 간 '일'의 차이 수를 구한다.
// 기준 날짜의 연/월/일
int YY = Integer.parseInt(today.split("\\.")[0]);
int MM = Integer.parseInt(today.split("\\.")[1]);
int DD = Integer.parseInt(today.split("\\.")[2]);
.
.
// 기준 날짜와 현재 개인정보 수집일자 간 '일' 차이 구하기
int num = (YY - Integer.parseInt(numbers[0])) * 28 * 12
+ (MM - Integer.parseInt(numbers[1])) * 28
+ (DD - Integer.parseInt(numbers[2]));
- 구한 차이값이 보관가능한 개인정보 만료일(=개월 수 * 28)보다 크다면, 파기해야 할 개인정보 번호 배열에 값을 저장한다.
if(num >= type) {
list.add(i + 1);
}
💥 유의사항
점(.) 기준으로 분리하고 싶을 때는 .split("\\.")
이어야 함
🔺 다른 풀이들
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
|
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> answer = new ArrayList<>();
Map<String, Integer> termMap = new HashMap<>();
int date = getDate(today);
for (String s : terms) {
String[] term = s.split(" ");
termMap.put(term[0], Integer.parseInt(term[1]));
}
for (int i = 0; i < privacies.length; i++) {
String[] privacy = privacies[i].split(" ");
// 개인정보 만료일이 주어진 날짜 이전이라면 파기해야 함
if (getDate(privacy[0]) + (termMap.get(privacy[1]) * 28) <= date) {
answer.add(i + 1);
}
}
return answer.stream().mapToInt(integer -> integer).toArray(); // 리스트를 배열로 변환
}
private int getDate(String today) {
String[] date = today.split("\\.");
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
return (year * 12 * 28) + (month * 28) + day;
}
}
|
cs |
💬 느낀 점
문자열... 분리하다가 집중력 분리.....
집중하십시오... 딱콩...
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V |
(참고)
[프로그래머스] 개인정보 수집 유효기간 - JAVA
문제 설명고객의 약관 동의를 얻어서 수집된 1~n번으로 분류되는 개인정보 n개가 있습니다. 약관 종류는 여러 가지 있으며 각 약관마다 개인정보 보관 유효기간이 정해져 있습니다. 당신은 각 개
velog.io
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/Lv. 2] 택배상자 (JAVA) (0) | 2023.11.26 |
---|---|
[프로그래머스/Lv. 2] [1차] 뉴스 클러스터링 (JAVA) (0) | 2023.11.26 |
[프로그래머스/Lv. 1] 성격 유형 검사하기 (JAVA) (0) | 2023.11.22 |
[프로그래머스/Lv. 2] 주차 요금 계산 (JAVA) (0) | 2023.11.21 |
[프로그래머스/Lv. 2] 행렬 테두리 회전하기 (JAVA) (0) | 2023.11.21 |