<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cost Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/cost/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/cost/</link>
	<description></description>
	<lastBuildDate>Sun, 27 Jan 2019 21:50:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>cost Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/cost/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱LeetCode 983. Minimum Cost For Tickets</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Jan 2019 17:18:37 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[cost]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[optimization]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4699</guid>

					<description><![CDATA[<p>In a country popular for train travel, you&#160;have planned some train travelling one year in advance.&#160; The days of the year that you will travel&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/">花花酱LeetCode 983. Minimum Cost For Tickets</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>Train tickets are sold in 3 different ways:</p>



<ul><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>



<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>



<p>Return the minimum number of dollars you need to travel every day in the given list of&nbsp;<code>days</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>days = [1,4,6,7,8,20], costs = [2,7,15]
<strong>Output: </strong>11
<strong>Explanation: </strong>
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.
</pre>



<p><strong>Example 2:</strong></p>



<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]
<strong>Output: </strong>17
<strong>Explanation: </strong>
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.
</pre>



<p><strong>Note:</strong></p>



<ol><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>



<h2>Solution: DP</h2>



<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>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 0 ms
class Solution {
public:
  int mincostTickets(vector&lt;int&gt;&amp; days, vector&lt;int&gt;&amp; costs) {
    vector&lt;int&gt; req(days.back() + 1);
    vector&lt;int&gt; dp(days.back() + 1);
    for (int day : days) req[day] = 1;
    dp[0] = 0;
    for (int i = 1; i &lt; dp.size(); ++i) {
      if (!req[i]) {
        dp[i] = dp[i - 1];
        continue;
      }
      dp[i] = dp[i - 1] + costs[0];
      dp[i] = min(dp[i], dp[max(0, i - 7)] + costs[1]);
      dp[i] = min(dp[i], dp[max(0, i - 30)] + costs[2]);
    }
    return dp.back();
  }
};</pre>

</div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 60 ms
class Solution:
  def mincostTickets(self, days: 'List[int]', costs: 'List[int]') -&gt; 'int':
    req = set(days)
    dp = [0] * (days[-1] + 1)    
    for i in range(1, len(dp)):
      if not i in req: 
        dp[i] = dp[i - 1]
        continue
      dp[i] = min(dp[max(0, i - 1)] + costs[0],
                  dp[max(0, i - 7)] + costs[1],
                  dp[max(0, i - 30)] + costs[2])
    return dp[-1]</pre>
</div></div>



<p>               </p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/">花花酱LeetCode 983. Minimum Cost For Tickets</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
