Minimum Cost For Tickets LeetCode solution in JavaScript

1
var mincostTickets = function (days, costs) {
2
let store = {};
3
for (let i of days) {
4
store[i] = true;
5
}
6
let lastDay = days[days.length - 1];
7
let dp = new Array(days[days.length - 1] + 1).fill(Infinity);
8
dp[0] = 0;
9

10
for (let i = 1; i < days[days.length - 1] + 1; i++) {
11
if (!store[i]) {
12
dp[i] = dp[i - 1];
13
continue;
14
}
15
dp[i] = costs[0] + dp[i - 1];
16
dp[i] = Math.min(costs[1] + dp[Math.max(i - 7, 0)], dp[i]);
17
dp[i] = Math.min(costs[2] + dp[Math.max(i - 30, 0)], dp[i]);
18
}
19

20
return dp[lastDay];
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0