

점수를 기준으로 내림차순 정렬을 하는 것이 포인트!
N으로 과제의 개수를 저장하고, works 배열에 2차원 배열로 각 과제별 마감일과 점수를 저장한다.works 배열을 점수를 기준으로 내림차순 정렬하여 sortedWorks 배열로 저장한다.plan 배열을 과제의 개수만큼 크기를 지정하고, 0으로 초기화한다.sortedWorks 배열에서 순서대로 가져오면서 index에 "마감일 - 1"을 저장한다. plan[index]의 값이 0이면 해당 index에 과제의 점수를 넣는다. 0이 아니라면, index가 0이 되거나, plan[index]의 값이 0일때까지 index 값을 줄여나간다. plan[index]의 값이 0이라면 과제의 점수를 넣고 break한다.plan에 저장된 값들을 모두 더한다.const input = require("fs")
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
.toString()
.trim()
.split("\n")
.map((el) => el.split(" "));
const N = Number(input[0][0]);
let works = input.slice(1, N + 1).map((row) => row.map(Number));
const sortedWorks = works.sort((a, b) => b[1] - a[1]);
let plan = Array(N).fill(0);
sortedWorks.forEach((work) => {
let index = work[0]-1;
if(plan[index] === 0){
plan[index] = work[1];
}else{
while(index >= 0){
if(plan[index] === 0){
plan[index] = work[1];
break;
}else{
index--;
}
}
}
})
console.log(plan.reduce((acc, cur) => acc+cur, 0));
🤔 소감