[JAVA 문제 풀이] 320. 개인정보 수집 유효기간

프로그래머스 (150370)
Stupefyee's avatar
Jun 16, 2025
[JAVA 문제 풀이] 320. 개인정보 수집 유효기간
notion image
notion image
 

내가 작성한 코드

import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; class Solution { public int[] solution(String today, String[] terms, String[] privacies) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd"); // 날짜 포맷 지정 LocalDate todayDate = LocalDate.parse(today, formatter); // 약관 종류와 유효기간을 매핑 Map<String, Integer> termMap = new HashMap<>(); // 약관 종류와 유효기간을 매핑 List<Integer> answer = new ArrayList<>(); // 정답을 답안 리스트에 담기 // 약관 종류와 유효기간을 매핑 for (String term : terms) { String[] parts = term.split(" "); String type = parts[0]; int duration = Integer.parseInt(parts[1]); termMap.put(type, duration); } for(int i = 0; i < privacies.length; i++) { LocalDate privacyDate = LocalDate.parse(privacies[i].split(" ")[0], formatter); // 개인정보 수집 날짜 String termType = privacies[i].split(" ")[1]; // 약관 종류 int duration = termMap.get(termType); // 약관 종류에 해당하는 유효기간 LocalDate expirationDate = privacyDate.plusMonths(duration); // 유효기간 계산 if (expirationDate.isBefore(todayDate) || expirationDate.isEqual(todayDate)) { answer.add(i + 1); } } return answer.stream() .mapToInt(Integer::intValue) .toArray(); } }
 

다른 사람의 코드

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; } }
 
Share article

stupefyee