原文地址:http://www.javashuo.com/article/p-xtcynxoy-md.html html
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days
. Each day is an integer from 1
to 365
.數組
Train tickets are sold in 3 different ways:spa
costs[0]
dollars;costs[1]
dollars;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: day 2, 3, 4, 5, 6, 7, and 8.code
Return the minimum number of dollars you need to travel every day in the given list of days
. htm
Example 1:blog
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:get
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.
Note:input
1 <= days.length <= 365
1 <= days[i] <= 365
days
is in strictly increasing order.costs.length == 3
1 <= costs[i] <= 1000
在一個火車旅行很受歡迎的國度,你提早一年計劃了一些火車旅行。在接下來的一年裏,你要旅行的日子將以一個名爲 days
的數組給出。每一項是一個從 1
到 365
的整數。it
火車票有三種不一樣的銷售方式:io
costs[0]
美圓;costs[1]
美圓;costs[2]
美圓。通行證容許數天無限制的旅行。 例如,若是咱們在第 2 天得到一張爲期 7 天的通行證,那麼咱們能夠連着旅行 7 天:第 2 天、第 3 天、第 4 天、第 5 天、第 6 天、第 7 天和第 8 天。
返回你想要完成在給定的列表 days
中列出的每一天的旅行所須要的最低消費。
示例 1:
輸入:days = [1,4,6,7,8,20], costs = [2,7,15] 輸出:11 解釋: 例如,這裏有一種購買通行證的方法,能夠讓你完成你的旅行計劃: 在第 1 天,你花了 costs[0] = $2 買了一張爲期 1 天的通行證,它將在第 1 天生效。 在第 3 天,你花了 costs[1] = $7 買了一張爲期 7 天的通行證,它將在第 3, 4, ..., 9 天生效。 在第 20 天,你花了 costs[0] = $2 買了一張爲期 1 天的通行證,它將在第 20 天生效。 你總共花了 $11,並完成了你計劃的每一天旅行。
示例 2:
輸入:days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15] 輸出:17 解釋: 例如,這裏有一種購買通行證的方法,能夠讓你完成你的旅行計劃: 在第 1 天,你花了 costs[2] = $15 買了一張爲期 30 天的通行證,它將在第 1, 2, ..., 30 天生效。 在第 31 天,你花了 costs[0] = $2 買了一張爲期 1 天的通行證,它將在第 31 天生效。 你總共花了 $17,並完成了你計劃的每一天旅行。
提示:
1 <= days.length <= 365
1 <= days[i] <= 365
days
按順序嚴格遞增costs.length == 3
1 <= costs[i] <= 1000
124ms
1 class Solution { 2 func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int { 3 var n:Int = days.count 4 var dp:[Int] = [Int](repeating:Int.max / 2,count:n+1) 5 dp[0] = 0 6 for i in 1...n 7 { 8 dp[i] = dp[i-1] + costs[0] 9 for j in (0...(i - 1)).reversed() 10 { 11 if days[i-1] - days[j] + 1 <= 7 12 { 13 dp[i] = min(dp[i], dp[j] + costs[1]) 14 } 15 if days[i-1] - days[j] + 1 <= 30 16 { 17 dp[i] = min(dp[i], dp[j] + costs[2]) 18 } 19 } 20 } 21 return dp[n] 22 } 23 }