준영이의 성장일기(FrontEnd)

프로그래머스 2023 KAKAO BLIND RECRUITMENT 본문

코테 및 알고리즘

프로그래머스 2023 KAKAO BLIND RECRUITMENT

Jay_Jung 2024. 5. 21. 14:30

2024년 카카오 겨울 인턴십 1번 문제와 비슷한 유형이지만 난이도 면에서 비교적 쉬운 문제를 풀어보았다. 이번 문제 또한 프로그래머스 기반의 문제❗❗

 

<문제>

https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

 

 

 

 

 

<문제 핵심>

 

1. 객체를 활용하여 약관문자에 따른 유효기간을 키:값 형태로 저장하기

2. 오늘 날짜와 파싱해서 나온 날짜와의 비교를 통해 파기해야 할 정보를 배열로 저장

-> 유효기간이 지난 날짜는 파기해야 하는 정보로 취급

 

 

<해결방법 및 순서도>

 

1. 오늘 날짜를 구하고 termsObj라는 객체를 초기화 한다.

-> .을 기준으로 배열로 만든 뒤 문자열로 전환

 const todayDate = Number(today.split(".").join(""));
  let termsObj = {};

 

2. terms에 forEach문을 이용하여 순회하고 약관 문자에 따른 유효기간을 termsObj에 파싱한다.

-> 키:값 형태로 파싱 진행

-> 파기된 정보를 담을 ans배열을 초기화 한다.

terms.forEach((term, index) => {
    const [contract, month] = term.split(" ");
    termsObj[contract] = Number(month);
  });
  
 let ans = []

 

3. 개인정보가 담긴 privacies 배열을 forEach문을 통회 순회하고 유효기간을 계산한 날짜와 오늘 날짜를 비교하여 유효기간이 계산된 날짜가 오늘 날짜보다 작거나 같으면 파기해야 하는 정보이므로 ans 배열에 push 한다.

 

<오늘 날짜가 예를 들어 2022.05.19 일 때>

 

 

✅2021.05.02를 기준으로 6개월 후인 유효기간 인정 날짜는  2021.11.01 이지만 오늘 날은 2022.05.19 이므로 유효기간이 지난 정보이다.

 

-> Month가 12를 넘으면 1월,2월 이런 형태로 가공해주기

-> month, day는 앞에 숫자 0이 들어간 형태여야 하므로 padStart 구문 사용

privacies.forEach((privacy, index) => {
    let [date, contract] = privacy.split(" ");
    let [year, month, day] = date.split(".").map(Number);
    month += termsObj[contract];

    while (month > 12) {
      year += 1;
      month -= 12;
    }

    year = String(year);
    month = String(month).padStart(2, "0");
    day = String(day).padStart(2, "0");

    const caculatedTime = Number(year + month + day);
    if (caculatedTime <= todayDate) {
      ans.push(index + 1);
    }
  });

 

 

<전체 코드>

 

function solution(today, terms, privacies) {
  const todayDate = Number(today.split(".").join(""));
  let termsObj = {};
  terms.forEach((term, index) => {
    const [contract, month] = term.split(" ");
    termsObj[contract] = Number(month);
  });
  let ans = [];
  privacies.forEach((privacy, index) => {
    let [date, contract] = privacy.split(" ");
    let [year, month, day] = date.split(".").map(Number);
    month += termsObj[contract];

    while (month > 12) {
      year += 1;
      month -= 12;
    }

    year = String(year);
    month = String(month).padStart(2, "0");
    day = String(day).padStart(2, "0");

    const caculatedTime = Number(year + month + day);
    if (caculatedTime <= todayDate) {
      ans.push(index + 1);
    }
  });
  return ans;
}

'코테 및 알고리즘' 카테고리의 다른 글

2022 KAKAO BLIND RECRUITMENT  (0) 2024.05.24
2022 KAKAO TECH INTERNSHIP 문제  (0) 2024.05.23
프로그래머스 2024 KAKAO WINTER INTERNSHIP 1번  (0) 2024.05.19
백준 js 1406번  (0) 2024.05.13
백준 js 2805번  (0) 2024.04.29