-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1463.js
42 lines (32 loc) · 794 Bytes
/
1463.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
const DP = []
DP[1] = 0;
DP[2] = 1;
DP[3] = 1;
function main(input) {
const N = Number(input[0]);
let two, three, one
for (let i = 3; i <= N; i++) {
let two = Infinity, three= Infinity, one= Infinity
if (i % 2 === 0) {
two = DP[2] + DP[Math.floor(i / 2)]
}
if (i % 3 === 0) {
three = DP[3] + DP[Math.floor(i / 3)]
}
one = DP[i] = DP[i - 1] + 1;
DP[i] = Math.min.apply(null, [two, three, one])
}
console.log(DP[N]);
}