https://school.programmers.co.kr/learn/courses/30/lessons/155651
if (newBookTime[i].length === 3) {
continue;
}
room += 1;
newBookTime[i].push(true);
const toMinute = (array) => {
return array
.map((time) =>
time.map((value, index) => {
const times = value.split(":");
if (index === 0) {
return 60 * Number(times[0]) + Number(times[1]);
}
return 60 * Number(times[0]) + Number(times[1]) + 10;
})
)
.sort((a, b) => a[0] - b[0]);
};
const solution = (book_time) => {
const newBookTime = toMinute(book_time);
let room = 0;
for (let i = 0; i < newBookTime.length; i += 1) {
if (newBookTime[i].length === 3) {
continue;
}
room += 1;
newBookTime[i].push(true);
let end = newBookTime[i][1];
let j = i + 1;
while (j < newBookTime.length) {
if (i === newBookTime.length - 1) {
break;
}
if (newBookTime[j][0] - end >= 0 && newBookTime[j].length !== 3) {
newBookTime[j].push(true);
end = newBookTime[j][1];
}
j += 1;
}
}
return room;
};
https://school.programmers.co.kr/learn/courses/30/lessons/148653#
const solution = (storey) => {
let answer = 0;
while (storey) {
let cur = storey % 10;
let next = Math.floor(storey / 10) % 10;
if (cur > 5) {
answer += 10 - cur;
storey += 10;
} else if (cur == 5) {
answer += 5;
storey += next >= 5 ? 10 : 0;
} else answer += cur;
storey = Math.floor(storey / 10);
}
return answer;
};
https://school.programmers.co.kr/learn/courses/30/lessons/136797
DP[n][l][r] = Math.min(DP[n-1][k][r], DP[n-1][l][k])
if (i === j || prevValue === Infinity) continue;
const weights = [
[1, 7, 6, 7, 5, 4, 5, 3, 2, 3],
[7, 1, 2, 4, 2, 3, 5, 4, 5, 6],
[6, 2, 1, 2, 3, 2, 3, 5, 4, 5],
[7, 4, 2, 1, 5, 3, 2, 6, 5, 4],
[5, 2, 3, 5, 1, 2, 4, 2, 3, 5],
[4, 3, 2, 3, 2, 1, 2, 3, 2, 3],
[5, 5, 3, 2, 4, 2, 1, 5, 3, 2],
[3, 4, 5, 6, 2, 3, 5, 1, 2, 4],
[2, 5, 4, 5, 3, 2, 3, 2, 1, 2],
[3, 6, 5, 4, 5, 3, 2, 4, 2, 1],
];
const solution = (numbers) => {
const DP = Array.from({ length: numbers.length + 1 }, () =>
Array.from({ length: 10 }, () => new Array(10).fill(Infinity))
);
DP[0][4][6] = 0;
for (let idx = 0; idx < numbers.length; idx += 1) {
const num = numbers[idx];
const prevDP = DP[idx];
const nowDP = DP[idx + 1];
for (let i = 0; i < 10; i += 1) {
for (let j = 0; j < 10; j += 1) {
const prevValue = prevDP[i][j];
if (i === j || prevValue === Infinity) continue;
if (nowDP[num][j] > prevValue + weights[i][num]) {
nowDP[num][j] = prevValue + weights[i][num]
}
if (nowDP[i][num] > prevValue + weights[num][j]) {
nowDP[i][num] = prevValue + weights[num][j]
}
}
}
}
return Math.min(...DP[numbers.length].flat(2));
};