算法: 门票最低成本983. Minimum Cost For Tickets

Posted AI架构师易筋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法: 门票最低成本983. Minimum Cost For Tickets相关的知识,希望对你有一定的参考价值。

983. Minimum Cost For Tickets

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:

  • a 1-day pass is sold for costs[0] dollars,
  • a 7-day pass is sold for costs[1] dollars, and
  • a 30-day pass is sold for costs[2] dollars.

The passes allow that many days of consecutive travel.

  • For example, if we get a 7-day pass on day 2, then we can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8.
    Return the minimum number of dollars you need to travel every day in the given list of days.

Example 1:

Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total, you spent $11 and covered all the days of your travel.

Example 2:

Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total, you spent $17 and covered all the days of your travel.

Constraints:

  • 1 <= days.length <= 365
  • 1 <= days[i] <= 365
  • days is in strictly increasing order.
  • costs.length == 3
  • 1 <= costs[i] <= 1000

动态规划解法

dp[i] 表示最多第 i 天的门票最低费用。dp 数组的大小取决于最后一个旅行日,因此我们不需要数组长度为 365。

我们确实需要一个布尔数组来标记旅行天数,原因是如果它不是旅行日,我们就不需要票。但是,如果是旅行日,我们会考虑三种情况(具有三种类型的票证):

  1. 如果第 i 天的 1 天票,dp[i] = dp[i - 1] + cost[0]
  2. 如果第 i 天结束的 7 天票,dp[i] = min(dp[i - 7] , dp[i - 6] ... dp[i - 1]) + cost[1]
  3. 如果 30 天票在第 i 天结束,dp[i] = min(dp[i - 30], dp[i] - 29] ... dp[i - 1]) + cost[2]

但是由于 dp 数组的值在增加,因此:

  • 对于在第 i 天结束的 7 天票,dp[i] = dp[i - 7] + cost[1]
  • 对于在第 i 天结束的 30 天票, dp[i] = dp[i - 30] + cost[2]
 public int mincostTickets(int[] days, int[] costs) 
    // length up to the last travel + 1 day is good enough (no need for 365)
    int lastDay = days[days.length - 1]; 
    // dp[i] means up to i-th day the minimum cost of the tickets
    int[] dp = new int[lastDay + 1]; 
    boolean[] isTravelDays = new boolean[lastDay + 1];
    // mark the travel days
    for(int day : days) isTravelDays[day] = true;
    
    for(int i = 1; i <= lastDay; i++) 
        if(!isTravelDays[i])  // no need to buy ticket if it is not a travel day
            dp[i] = dp[i - 1];
            continue;
        
        // select which type of ticket to buy
        dp[i] = costs[0] + dp[i - 1]; // 1-day
        dp[i] = Math.min(costs[1] + dp[Math.max(i - 7, 0)], dp[i]); // 7-day
        dp[i] = Math.min(costs[2] + dp[Math.max(i - 30, 0)], dp[i]); // 30-day
    
    return dp[lastDay];

参考

https://leetcode.com/problems/minimum-cost-for-tickets/discuss/227130/Java-DP-Solution-with-detailed-comment-and-explanation

以上是关于算法: 门票最低成本983. Minimum Cost For Tickets的主要内容,如果未能解决你的问题,请参考以下文章

力扣第983题 最低票价

力扣第983题 最低票价

LeetCode 983. 最低票价

LeetCode 983. 最低票价

Minimum Cost For Tickets

LeetCode | 983.最低票价(动态规划)