<?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>stairs Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/stairs/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/stairs/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:26:56 +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>stairs Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/stairs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 746. Min Cost Climbing Stairs</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-746-min-cost-climbing-stairs/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-746-min-cost-climbing-stairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Dec 2017 17:30:40 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[stairs]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1271</guid>

					<description><![CDATA[<p>Problem: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-746-min-cost-climbing-stairs/">花花酱 LeetCode 746. Min Cost Climbing Stairs</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><iframe width="500" height="375" src="https://www.youtube.com/embed/v3WqNLmmBdk?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>On a staircase, the <code>i</code>-th step has some non-negative cost <code>cost[i]</code> assigned (0 indexed).</p>
<p>Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].</pre><p><b>Note:</b></p>
<ol>
<li><code>cost</code> will have a length in the range <code>[2, 1000]</code>.</li>
<li>Every <code>cost[i]</code> will be an integer in the range <code>[0, 999]</code>.</li>
</ol>
<p><strong>题目大意:</strong></p>
<p>有一个楼梯，要离开i层需要付cost[i]的费用，每次可以爬1层或2层。问你最少花多少钱能够达到顶楼。</p>
<p><strong>Solution:</strong></p>
<p>C++ / Recursion + Memorization 记忆化递归</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {
        vector&lt;int&gt; m(cost.size() + 1);
        return dp(cost, m, cost.size());
    }
private:
    // min cost to climb to i-th step
    int dp(vector&lt;int&gt;&amp; cost, vector&lt;int&gt;&amp; m, int i) {
        if (i &lt;= 1) return 0;        
        if (m[i] &gt; 0) return m[i];
        return m[i] = min(dp(cost, m, i - 1) + cost[i - 1], 
                          dp(cost, m, i - 2) + cost[i - 2]);
    }
};</pre><p></p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {
        vector&lt;int&gt; m(cost.size() + 1);
        return min(dp(cost, m, cost.size() - 1),
                   dp(cost, m, cost.size() - 2));
    }
private:
    // min cost before leaving i-th step
    int dp(vector&lt;int&gt;&amp; cost, vector&lt;int&gt;&amp; m, int i) {
        if (i &lt; 0) return 0;        
        if (m[i] &gt; 0) return m[i];
        return m[i] = min(dp(cost, m, i - 1), dp(cost, m, i - 2)) + cost[i];
    }
};</pre><p>&nbsp;</p>
<p>C++ / DP 动态规划</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {
        const int n = cost.size();
        vector&lt;int&gt; dp(n, INT_MAX); // d[i] min cost before leaving i
        dp[0] = cost[0];
        dp[1] = cost[1];
        for (int i = 2; i &lt; n; ++i)
            dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
        // We can reach top from either n - 1, or n - 2
        return min(dp[n - 1], dp[n - 2]);
    }
};</pre><p></p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 12 ms
class Solution {
public:
    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {
        // dp[i] : min cost to climb to n-th step
        vector&lt;int&gt; dp(cost.size() + 1, 0);
        for (int i = 2; i &lt;= cost.size(); ++i)
            dp[i] = min(dp[i - 1] + cost[i - 1],
                        dp[i - 2] + cost[i - 2]);
        return dp[cost.size()];
    }
};</pre><p>O(1) Space</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 15 ms
class Solution {
public:
    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {        
        int dp1 = 0;
        int dp2 = 0;
        for (int i = 2; i &lt;= cost.size(); ++i) {
            int dp = min(dp1 + cost[i - 1], dp2 + cost[i - 2]);
            dp2 = dp1;
            dp1 = dp;
        }
        return dp1;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-746-min-cost-climbing-stairs/">花花酱 LeetCode 746. Min Cost Climbing Stairs</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-746-min-cost-climbing-stairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
