983. Minimum Cost For Tickets
Difficulty: Medium
Topics: Array, Dynamic Programming
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 days. Each day is an integer from 1 to 365.
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:
Solution:
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.
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) { ... ... ... /** * go to ./solution.php */ } // 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 ?> <h3> Explanation: </h3> <ul> <li>The algorithm iterates over each day of the year (365 days).</li> <li>For each travel day, it computes the cost by considering whether it is cheaper to: <ul> <li>Buy a 1-day pass (adds the cost of the 1-day pass to the previous day's cost).</li> <li>Buy a 7-day pass (adds the cost of the 7-day pass and considers the cost of traveling on the past 7 days).</li> <li>Buy a 30-day pass (adds the cost of the 30-day pass and considers the cost of traveling over the past 30 days).</li> </ul> </li> <li>If it is not a travel day, the cost remains the same as the previous day.</li> </ul> <h3> Example Walkthrough </h3> <h4> Example 1: </h4> <p><strong>Input:</strong><br> </p> <pre class="brush:php;toolbar:false">$days = [1, 4, 6, 7, 8, 20]; $costs = [2, 7, 15];
Total cost = $2 $7 $2 = $11.
Input:
$days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31]; $costs = [2, 7, 15];
Total cost = $15 $2 = $17.
The 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.
$days = [1, 4, 6, 7, 8, 20]; $costs = [2, 7, 15]; echo mincostTickets($days, $costs); // Output: 11
$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.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of . Minimum Cost For Tickets. For more information, please follow other related articles on the PHP Chinese website!