개발 무지렁이

[문제풀이] P150370 개인정보 수집 유효기간 본문

코딩 테스트/문제풀이

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

Gaejirang-e 2023. 6. 26. 16:55

개인정보 수집 유효기간 / 구현문제 🚀


  🪅. dot(.)을 기준으로 문자열을 split할 때, 마침표를 표현할 수 있어야 한다.
         => \는 확장문자로 뒤에 일반문자를 특수문자로, 특수문자를 '그 문자 자체'로 인식한다.
         => \. 이 덩어리가 마침표를 의미함으로, 앞에 \를 하나 더 붙여 뒤에 \를 그 문자 자체로 인식하게 한다.
         => \\.
  🪅. 'Map'으로 데이터를 연결하여 '정보에 대응되는 또 다른 정보'를 이용할 수 있어야 한다.
  🪅. List를 'stream()'을 이용해서 바로 'int 배열'로 변환할 수 있으면 좋다 => stream().mapToInt(Integer::intValue).toArray();
  import java.util.*;
  class Solution {
      Map<String, Integer> map = new HashMap<>();
      String gToday;
      List<Integer> result = new ArrayList<>();

      public boolean checkValid(int year, int month, int day) {
          String[] todayBits = gToday.split("\\.");
          int tYear = Integer.parseInt(todayBits[0]);
          int tMonth = Integer.parseInt(todayBits[1]);
          int tDay = Integer.parseInt(todayBits[2]);
          if(tYear > year) return true;
          else if(tYear < year) return false;
          else { //년도가 같을 때
              if(tMonth < month) {
                  return false;
              } else if(tMonth > month) {
                  return true;
              } else { //월이 같을때
                  if(tDay >= day) {
                      return true;
                  } else {
                      return false;
                  }
              }
          }
      }
      public void calPeriod(String[] privacies) {
          int idx = 1;
          for(String privacy : privacies) {
              String[] privacyBits = privacy.split(" ");    
              String date = privacyBits[0];

              int plusPeriod = map.get(privacyBits[1]);
              int plusYear = plusPeriod/12;
              int plusMonth = plusPeriod%12;

              String[] dateBits = date.split("\\.");
              int year = Integer.parseInt(dateBits[0]);
              int month = Integer.parseInt(dateBits[1]);
              int day = Integer.parseInt(dateBits[2]);

              if(month + plusMonth > 12) {
                  year++;
                  month = month+plusMonth - 12;
              } else {
                  month += plusMonth;
              }

              year += plusYear;

              // System.out.println(year + " " + month + " " + day);
              if(checkValid(year, month, day)) {
                  result.add(idx);         
              }
              idx++;
          }
      }
      public void insertMap(String[] terms) {
          for(String term : terms) {
              String[] termBits = term.split(" ");
              String kind = termBits[0];
              String period = termBits[1];
              // System.out.println(kind + ": " + period);

              map.put(kind, Integer.parseInt(period));
          }
      }
      public int[] solution(String today, String[] terms, String[] privacies) {
          gToday = today;
          insertMap(terms);
          calPeriod(privacies);
          System.out.println(result);
          int[] answer = result.stream().mapToInt(Integer::intValue).toArray();
          return answer;
      }
  }

💡. 문제 접근 방법
: 입력으로 주어진 정보를 통해 관련 정보를 연결시켜 계산해야 했을 때, 자료구조 Map이 떠올랐고,
이런 식으로 문제를 해결하는게, 좋은 방식임을 느꼈다.
다만, 헤맸던 부분은 '약관에 따른 유효기간'의 최대값을 12달로 봤던 것이, 몇몇 테스트케이스를
통과하지 못하게 만들었다. (24달, 35달.. 등등 얼마든지 가능하다.)
(이는 문제 조건에 1이상 100이하로 범위가 주어진다.)

Comments