문제 🔽
https://school.programmers.co.kr/learn/courses/30/lessons/150370
다른사람 풀이 🔽
function solution(today, terms, privacies) {
var answer = [];
var [year, month, date] = today.split(".").map(Number);
var todates = year * 12 * 28 + month * 28 + date;
var t = {};
terms.forEach((e) => {
let [a, b] = e.split(" ");
t[a] = Number(b);
});
privacies.forEach((e, i) => {
var [day, term] = e.split(" ");
day = day.split(".").map(Number);
var dates = day[0] * 12 * 28 + day[1] * 28 + day[2] + t[term] * 28;
if (dates <= todates) answer.push(i + 1);
});
return answer;
}
내풀이🔽
1. 성공 ⭕
function solution(today, terms, privacies) {
// 오늘 기준 년도, 달, 날짜
let [year,month,date] = today.split(".").map((val)=> Number(val))
// ex) newTerms[A]=6
let newTerms = {}
terms.forEach((val)=>{
let [name, term] = val.split(" ")
newTerms[name]=Number(term)
})
// ex) [[2019,1,1,'D']....]
privacies=privacies.map((val)=>{
let arr = []
arr.push(Number(val.substring(0,4)))
arr.push(Number(val.substring(5,7)))
arr.push(Number(val.substring(8,10)))
arr.push(val.split(" ")[1])
return arr
})
let answer=[]
// ex) val=[2021,1,5,"A"]
privacies.forEach((val,index)=>{
// @type number ex)0,1,2,3 ...
let gap = year-val[0]
if(month+gap*12-val[1]>newTerms[val[3]]){
answer.push(index+1)
}else if(month+gap*12-val[1]===newTerms[val[3]]){
if(date>=val[2]){answer.push(index+1)}
}
})
return answer;
}
느낀점🔽
다른 사람들의 풀이를 보아하니 날짜 비교를 각 해당 날짜를 일수로 변환한 다음에 비교하였다.
예를들어
2023.7.23 => 2023*12*28 + 7*28 + 23
2022.6.23 => 2022*12*28 + 6*28 + 23
이런식으로 변환하여 문서의 파기를 결정했다.
한편, 나는 년, 월, 일을 일일히 비교 했는데 위와 같은 방식으로 하면 케이스별 분기를 나누지 않아도 되어 편해보였다.
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 약수의 개수와 덧셈(lv1) (0) | 2023.07.26 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 삼총사(lv1) (0) | 2023.07.25 |
프로그래머스 코딩테스트 풀이(js) > 최대공약수와 최소공배(lv1) (0) | 2023.07.23 |
프로그래머스 코딩테스트 풀이(js) > 숫자 짝꿍(lv1) (0) | 2023.07.22 |
프로그래머스 코딩테스트 풀이(js) > 행렬의 덧셈(lv1) (0) | 2023.07.21 |