Skip to content

Commit

Permalink
Create mincosttraveldays.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dalipkumar703 authored May 9, 2024
1 parent 9730679 commit c4d408f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions mincosttraveldays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} days
* @param {number[]} costs
* @return {number}
*/
var mincostTickets = function(days, costs) {
let dp = [];
for (i = 1 ; i < days[days.length -1 ]; i++){
dp[i] = 0;
}
for( i = 1 ; i < dp.length;i++){
//if not travelling this day
if (!days.includes(i)){
dp[i] = dp[i-1]
} else{
dp[i] = Math.min(costs[0]+dp[i-1], costs[1]+dp[Math.max(0,i - 7)], costs[2]+dp[Math.max(0,i - 30)]);
}
}
return dp[dp.length - 1]
};

0 comments on commit c4d408f

Please sign in to comment.