Leetcode of dynamic programming (DP) Subject - Detailed 983. The lowest fare (Minimum Cost For Tickets)

Leetcode of dynamic programming (DP) Thematic -983 lowest fare (Minimum Cost For Tickets)


 

In a very popular country train travel, you plan one year ahead of a number of train travel. Over the next year, the day you want to travel will be named  days array is given. Each is from a  1 to  365 integer.

There are three different train ticket sales:

  • A one-day pass is priced at  costs[0] US $;
  • A seven-day pass is priced at  costs[1] US $;
  • A 30-day pass is priced  costs[2] in US dollars.

Pass allows unlimited travel for several days. For example, if we get a seven-day pass on day 2, then we can travel attached to 7 days: on day 2, day 3, day 4, day 5, day 6, 7 and day 8.

Return you want to accomplish in a given list of  days minimum consumption listed travel every day as needed.

 

Example 1:

Input: days = [1,4,6,7,8,20], costs = [2,7,15] 
Output: 11 
Explanation: 
For example, there is a method of buying a pass that allows you to complete your travel plans : 
on day 1, you spent costs [0] = $ 2 to buy a one-day pass, which will take effect on day 1. 
On Day 3, you spend costs [1] = $ 7 to buy a seven-day pass, which will enter into force on 3, 4, ..., 9 days. 
In the first 20 days, you spend costs [0] = $ 2 to buy a one-day pass, which will take effect in 20 days. 
You spent a total of $ 11, and every day you complete travel plans.

Example 2:

Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15] 
Output: 17 
Explanation: 
e.g., where there is a later pass way for you to complete your travel plan: 
on day 1, you spent costs [2] = $ 15 to buy a 30-day pass, it will be in 1, 2, ..., 30 days to take effect. 
In the first 31 days, you spend costs [0] = $ 2 to buy a one-day pass, which will take effect in 31 days. 
You spent a total of $ 17, and every day you complete travel plans.

 

prompt:

  1. 1 <= days.length <= 365
  2. 1 <= days[i] <= 365
  3. days Strictly increasing sequence
  4. costs.length == 3
  5. 1 <= costs[i] <= 1000

 

DP is defined:

The DP [i] represents from day 1 to day i-th lowest cost.

365 days a year, so that there might be 363 days, so a new array of size 366, as will be later calculated i-1, i-7, i-30, subscripts will overflow, so the array size is increased 396, and then all day all goes back 30 days to prevent overflow.

 

If the day without travel, and then take on the previous day is the same.

If one day's travel, then you can take in:

Spent 1,1 days before the day pass +

Spent 2,7 days before the weekly ticket +

3, 30 days before the monthly cost +

The minimum value, that is DP [i] a, i.e., minimum cost from day 1 to day i.

 

dp initialization time, the day marks have to travel about (I have here is to become a travel day -1, day trips is not 0), then the calculation.

 

Figure (in Example 1, for example, a red frame is the minimum cost of living):

 

 

Explain to 38 days, for example:

In the first 37 days, I spent 7

To 38 days, I'm going to travel, I have three kinds of programs:

Day ticket: Well, I can buy a day ticket on day 37, day 38 so that I can use to spend the first 37 days of the date of ticket money spent +

Weekly Pass: I can buy in one week on the 31st day ticket, valid for 31-38 days, and then I spent the first 31 days of the ticket money spent weeks +

Pass: Can I buy a monthly ticket on day 8, valid for the first 8 days - 38 days, I spent a total for the first eight days of the monthly cost +

...

And so on

 

 

class Solution {
    public int mincostTickets(int[] days, int[] costs) {
        int[] dp = new int[396];
        int dayCost = costs[0];
        int weekCost = costs[1];
        int monthCost = costs[2];

        for (int i = 0; i < days.length; i++) {
            dp[days[i]+30] = -1;
        }
        for (int i = 31; i <= 395; i++) {
            if(dp[i]==0){
                dp[i] = dp[i-1];
            }else{
                dp[i] = Math.min(Math.min(dp[i-1]+dayCost,dp[i-7]+weekCost),dp[i-30]+monthCost);
            }
        }
        return dp[days[days.length-1]+30];
    }
}

 

Guess you like

Origin www.cnblogs.com/qinyuguan/p/11481705.html