-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum_cost_for_tickets.js
39 lines (37 loc) · 1.52 KB
/
minimum_cost_for_tickets.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
// https://leetcode.com/problems/minimum-cost-for-tickets/description/
// 983. Minimum Cost For Tickets
/**
* @param {number[]} days
* @param {number[]} costs
* @return {number}
*/
var mincostTickets = function(days, costs) {
// Create an array to store the minimum cost for each day of the year
const minCosts = new Array(days[days.length - 1] + 1).fill(0);
// Create a set of the days you will be traveling on for faster lookup
const travelDays = new Set(days);
// Loop through each day of the year, starting from day 1
for (let day = 1; day < minCosts.length; day++) {
// If you're not traveling on this day, the minimum cost is the same as the previous day
if (!travelDays.has(day)) {
minCosts[day] = minCosts[day - 1];
} else {
// If you're traveling on this day, calculate the minimum cost by comparing the costs
// of buying a 1-day pass, a 7-day pass, or a 30-day pass on this day, and adding the
// cost of the minimum cost of traveling up to 1, 7, or 30 days ago respectively
minCosts[day] = Math.min(
minCosts[day - 1] + costs[0],
minCosts[Math.max(0, day - 7)] + costs[1],
minCosts[Math.max(0, day - 30)] + costs[2]
);
}
}
// Return the minimum cost for traveling on all the given days
return minCosts[minCosts.length - 1];
};
console.log(mincostTickets([1,4,6,7,8,20], [2,7,15]));
// Output: 11
console.log(mincostTickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,7,15]));
// Output: 17
console.log(mincostTickets([1,24,146], [2,7,15]));
// Output: 6