코테/프로그래머스
99클럽 코테 스터디 34일차 TIL 문자열(개인정보 수집 유효기간)
zsunny
2024. 11. 30. 15:14
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
<풀이 과정>
문제의 핵심은 기간을 비교하면 되는 것이다.
우선 약관종류에 따른 유효기간은 Map에 넣어 사용했고, 모든 비교를 일(days)로 바꿔 계산했다.
<제출 코드>
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
int year = Integer.parseInt(today.split("\\.")[0]);
int month = Integer.parseInt(today.split("\\.")[1]);
int day = Integer.parseInt(today.split("\\.")[2]);
int flag = (year * 12 * 28) + (month * 28) + day;
// 유효기간
HashMap<String, String> map = new HashMap<>();
for(int i=0; i<terms.length; i++){
map.put(terms[i].split(" ")[0], terms[i].split(" ")[1]);
}
// 개인정보 수집 만료일 확인
for(int i=0; i<privacies.length; i++){
String date = privacies[i].split(" ")[0];
int y = Integer.parseInt(date.split("\\.")[0]);
int m = Integer.parseInt(date.split("\\.")[1]);
int d = Integer.parseInt(date.split("\\.")[2]);
String type = privacies[i].split(" ")[1];
int sum = (y * 12 * 28) + (m * 28) + d + (Integer.parseInt(map.get(type)) * 28);
if(flag >= sum) pq.add(i+1);
}
int[] answer = new int[pq.size()];
int idx = 0;
while(!pq.isEmpty()){
answer[idx++] = pq.poll();
}
return answer;
}
}