983. Minimum Cost For Tickets #1038
-
Topics: You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array Train tickets are sold in three different ways:
The passes allow that many days of consecutive travel.
Return the minimum number of dollars you need to travel every day in the given list of days. Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem involves determining the minimum cost to travel on a set of specified days throughout the year. The problem offers three types of travel passes: 1-day, 7-day, and 30-day passes, each with specific costs. The goal is to find the cheapest way to cover all travel days using these passes. The task requires using dynamic programming to efficiently calculate the minimal cost. Key Points
Approach
Plan
Let's implement this solution in PHP: 983. Minimum Cost For Tickets <?php
/**
* @param Integer[] $days
* @param Integer[] $costs
* @return Integer
*/
function mincostTickets($days, $costs) {
// Initialize the dp array where dp[i] represents the minimum cost up to day i.
$dp = array_fill(0, 366, PHP_INT_MAX); // dp[0] = 0 by default
$dp[0] = 0; // Base case: no cost if we haven't traveled yet
// Set a set of days for faster lookup
$travelDays = array_flip($days); // Flip days to make it O(1) lookup
// Loop over all days from 1 to 365
for ($i = 1; $i <= 365; $i++) {
if (isset($travelDays[$i])) { // If we travel on this day
// 1-day pass: add the cost of a 1-day pass
$dp[$i] = min($dp[$i], $dp[$i-1] + $costs[0]);
// 7-day pass: add the cost of a 7-day pass (look up to day i-7)
$dp[$i] = min($dp[$i], $dp[max(0, $i-7)] + $costs[1]);
// 30-day pass: add the cost of a 30-day pass (look up to day i-30)
$dp[$i] = min($dp[$i], $dp[max(0, $i-30)] + $costs[2]);
} else {
// If we don't travel on this day, just carry forward the previous cost
$dp[$i] = $dp[$i-1];
}
}
// The answer will be the cost to cover all days, which is dp[365]
return $dp[365];
}
// Example usage:
$days1 = [1, 4, 6, 7, 8, 20];
$costs1 = [2, 7, 15];
echo mincostTickets($days1, $costs1); // Output: 11
$days2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31];
$costs2 = [2, 7, 15];
echo mincostTickets($days2, $costs2); // Output: 17
?> Explanation:
Example WalkthroughExample 1:Input: $days = [1, 4, 6, 7, 8, 20];
$costs = [2, 7, 15];
Total cost = $2 + $7 + $2 = $11. Example 2:Input: $days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31];
$costs = [2, 7, 15];
Total cost = $15 + $2 = $17. Time ComplexityThe time complexity of the solution is O(365), as we are iterating through all days of the year, and for each day, we perform constant time operations (checking travel days and updating the DP array). Thus, the solution runs in linear time relative to the number of days. Output for ExampleExample 1:$days = [1, 4, 6, 7, 8, 20];
$costs = [2, 7, 15];
echo mincostTickets($days, $costs); // Output: 11 Example 2:$days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31];
$costs = [2, 7, 15];
echo mincostTickets($days, $costs); // Output: 17 The solution efficiently calculates the minimum cost of covering the travel days using dynamic programming. By iterating over the days and considering all possible passes (1-day, 7-day, 30-day), the algorithm finds the optimal strategy for purchasing the passes. The time complexity is linear in terms of the number of days, making it suitable for the problem constraints. |
Beta Was this translation helpful? Give feedback.
The problem involves determining the minimum cost to travel on a set of specified days throughout the year. The problem offers three types of travel passes: 1-day, 7-day, and 30-day passes, each with specific costs. The goal is to find the cheapest way to cover all travel days using these passes. The task requires using dynamic programming to efficiently calculate the minimal cost.
Key Points
d
in thedays
array, calculate …