361. 개인정보 수집 유효기간

김해준's avatar
May 06, 2026
361. 개인정보 수집 유효기간

언어 : Java

문제

notion image
notion image
notion image
notion image
notion image

풀이 코드

import java.util.*; class Solution { public int[] solution(String today, String[] terms, String[] privacies) { int todayVal = convertTerm(today); String[] type = new String[terms.length]; int[] addVal = new int[terms.length]; for(int i = 0 ; i < terms.length; i++){ String[] sArr = terms[i].split(" "); type[i] = sArr[0]; addVal[i] = Integer.parseInt(sArr[1]) * 28; } int[] temp = new int[privacies.length]; int count = 0; for(int i = 0 ; i < privacies.length; i++){ String[] priArr = privacies[i].split(" "); int startDate = convertTerm(priArr[0]); String aType = priArr[1]; int expireDate = 0; for(int j = 0; j < type.length; j++){ if(type[j].equals(aType)){ expireDate = startDate + addVal[j] - 1; break; } } if(expireDate < todayVal){ temp[count++] = i + 1; } } int[] answer = new int[count]; for(int i = 0; i < count; i++){ answer[i] = temp[i]; } return answer; } public int convertTerm(String date){ int year = 28 * 12; int month = 28; String[] days = date.split("\\."); int result = 0; result += Integer.parseInt(days[0]) * year; result += Integer.parseInt(days[1]) * month; result += Integer.parseInt(days[2]); return result; } }

코드 해설

📌 전체 흐름 한눈에 보기

  1. today를 숫자로 변환
  1. 약관(terms)을 타입별 유효기간(일수)로 변환
  1. 개인정보(privacies)마다:
      • 시작일 → 숫자 변환
      • 약관 타입에 맞는 기간 더함
      • 만료일 계산
  1. 오늘 날짜와 비교해서 파기 대상인지 체크
  1. 결과 배열 반환

📌 1. 날짜를 숫자로 바꾸는 이유

public int convertTerm(String date){ int year=28*12; int month=28; String[] days = date.split("\\."); int result = 0; result += Integer.parseInt(days[0])*year; result += Integer.parseInt(days[1])*month; result += Integer.parseInt(days[2]); return result; }

💡 왜 이렇게 하냐?

이 문제는 특이하게
👉 모든 달이 28일로 고정
그래서 날짜를 이렇게 바꿀 수 있음:
2022.05.19 → (2022 * 12 * 28) + (5 * 28) + 19
👉 이렇게 하면
날짜 비교를 단순한 숫자 비교로 바꿀 수 있음

📌 2. 약관(term) 처리

String[]type = new String[terms.length]; int[]addVal = new int[terms.length];
String[]sArr = terms[i].split(" "); type[i] = sArr[0]; addVal[i] = Integer.parseInt(sArr[1])*28;

💡 의미

예를 들어:
"A 6"
👉 A 타입은 6개월
👉 6 * 28 = 168일
즉:
타입
기간
A
168일
B
...

📌 3. 개인정보 처리

for(int i = 0 ; i < privacies.length ; i++){

1️⃣ 시작일 변환

int startDate = convertTerm(priArr[0]);

2️⃣ 타입 확인

String aType = priArr[1];

3️⃣ 만료일 계산

expireDate = startDate+addVal[j]-1;

💡 여기 중요한 포인트

👉 -1 하는 이유
문제 조건:
"유효기간은 해당 날짜의 전날까지"
예:
2022.05.19 + 6개월 → 2022.11.18까지 유효
그래서:
start + 기간 - 1

📌 4. 파기 여부 판단

if(expireDate < todayVal){ temp[count++]=i+1; }

💡 조건 해석

👉 만료일 < 오늘
👉 이미 유효기간 지남 → 파기 대상

📌 5. 결과 배열 만들기

int[] answer = new int[count]; for(int i = 0 ; i < count ; i++){ answer[i]=temp[i]; }
👉 temp 배열에서 필요한 만큼만 잘라서 반환

📌 전체 핵심 정리

✅ 1. 날짜를 숫자로 변환

→ 비교 쉽게 만들기

✅ 2. 약관 기간을 "일" 단위로 통일

→ 계산 단순화

✅ 3. 만료일 = 시작일 + 기간 - 1

→ 문제 조건 정확히 반영

📌 시간 복잡도

  • terms: O(T)
  • privacies: O(P × T)
👉 최대:
O(N²) 수준이지만 데이터 작아서 충분
Share article