{"id":4699,"date":"2019-01-27T09:18:37","date_gmt":"2019-01-27T17:18:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4699"},"modified":"2019-01-27T13:50:22","modified_gmt":"2019-01-27T21:50:22","slug":"leetcode-983-minimum-cost-for-tickets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-983-minimum-cost-for-tickets\/","title":{"rendered":"\u82b1\u82b1\u9171LeetCode 983. Minimum Cost For Tickets"},"content":{"rendered":"\n<p>In a country popular for train travel, you&nbsp;have planned some train travelling one year in advance.&nbsp; The days of the year that you will travel is given as an array&nbsp;<code>days<\/code>.&nbsp; Each day is an integer from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>365<\/code>.<\/p>\n\n\n\n<p>Train tickets are sold in 3 different ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a 1-day pass is sold for&nbsp;<code>costs[0]<\/code>&nbsp;dollars;<\/li><li>a 7-day pass is sold for&nbsp;<code>costs[1]<\/code>&nbsp;dollars;<\/li><li>a 30-day pass is sold for&nbsp;<code>costs[2]<\/code>&nbsp;dollars.<\/li><\/ul>\n\n\n\n<p>The passes allow that many days of consecutive travel.&nbsp; 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.<\/p>\n\n\n\n<p>Return the minimum number of dollars you need to travel every day in the given list of&nbsp;<code>days<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>days = [1,4,6,7,8,20], costs = [2,7,15]\n<strong>Output: <\/strong>11\n<strong>Explanation: <\/strong>\nFor example, here is one way to buy passes that lets you travel your travel plan:\nOn day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.\nOn day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.\nOn day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.\nIn total you spent $11 and covered all the days of your travel.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]\n<strong>Output: <\/strong>17\n<strong>Explanation: <\/strong>\nFor example, here is one way to buy passes that lets you travel your travel plan:\nOn day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.\nOn day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.\nIn total you spent $17 and covered all the days of your travel.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= days.length &lt;= 365<\/code><\/li><li><code>1 &lt;= days[i] &lt;= 365<\/code><\/li><li><code>days<\/code>&nbsp;is in strictly increasing order.<\/li><li><code>costs.length == 3<\/code><\/li><li><code>1 &lt;= costs[i] &lt;= 1000<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: DP<\/h2>\n\n\n\n<p>dp[i] := min cost to cover the i-th day<br>dp[0] = 0<br>dp[i] = min(dp[i &#8211; 1] + costs[0], dp[i &#8211; 7] + costs[1], dp[i &#8211; 30] + costs[2])<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 0 ms\nclass Solution {\npublic:\n  int mincostTickets(vector<int>& days, vector<int>& costs) {\n    vector<int> req(days.back() + 1);\n    vector<int> dp(days.back() + 1);\n    for (int day : days) req[day] = 1;\n    dp[0] = 0;\n    for (int i = 1; i < dp.size(); ++i) {\n      if (!req[i]) {\n        dp[i] = dp[i - 1];\n        continue;\n      }\n      dp[i] = dp[i - 1] + costs[0];\n      dp[i] = min(dp[i], dp[max(0, i - 7)] + costs[1]);\n      dp[i] = min(dp[i], dp[max(0, i - 30)] + costs[2]);\n    }\n    return dp.back();\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n\/\/ Author: Huahua, running time: 60 ms\nclass Solution:\n  def mincostTickets(self, days: 'List[int]', costs: 'List[int]') -> 'int':\n    req = set(days)\n    dp = [0] * (days[-1] + 1)    \n    for i in range(1, len(dp)):\n      if not i in req: \n        dp[i] = dp[i - 1]\n        continue\n      dp[i] = min(dp[max(0, i - 1)] + costs[0],\n                  dp[max(0, i - 7)] + costs[1],\n                  dp[max(0, i - 30)] + costs[2])\n    return dp[-1]\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>               <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a country popular for train travel, you&nbsp;have planned some train travelling one year in advance.&nbsp; The days of the year that you will travel&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[455,18,177,431],"class_list":["post-4699","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-cost","tag-dp","tag-medium","tag-optimization","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4699"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4699\/revisions"}],"predecessor-version":[{"id":4705,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4699\/revisions\/4705"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}