{"id":1271,"date":"2017-12-17T09:30:40","date_gmt":"2017-12-17T17:30:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1271"},"modified":"2018-04-19T08:26:56","modified_gmt":"2018-04-19T15:26:56","slug":"leetcode-746-min-cost-climbing-stairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-746-min-cost-climbing-stairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 746. Min Cost Climbing Stairs"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 746. Min Cost Climbing Stairs - \u5237\u9898\u627e\u5de5\u4f5c EP137\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/v3WqNLmmBdk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>On a staircase, the\u00a0<code>i<\/code>-th step has some non-negative cost\u00a0<code>cost[i]<\/code>\u00a0assigned (0 indexed).<\/p>\n<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>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: cost = [10, 15, 20]\r\nOutput: 15\r\nExplanation: Cheapest is start on cost[1], pay that cost and go to the top.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]\r\nOutput: 6\r\nExplanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>cost<\/code>\u00a0will have a length in the range\u00a0<code>[2, 1000]<\/code>.<\/li>\n<li>Every\u00a0<code>cost[i]<\/code>\u00a0will be an integer in the range\u00a0<code>[0, 999]<\/code>.<\/li>\n<\/ol>\n<p><strong>\u9898\u76ee\u5927\u610f:<\/strong><\/p>\n<p>\u6709\u4e00\u4e2a\u697c\u68af\uff0c\u8981\u79bb\u5f00i\u5c42\u9700\u8981\u4ed8cost[i]\u7684\u8d39\u7528\uff0c\u6bcf\u6b21\u53ef\u4ee5\u722c1\u5c42\u62162\u5c42\u3002\u95ee\u4f60\u6700\u5c11\u82b1\u591a\u5c11\u94b1\u80fd\u591f\u8fbe\u5230\u9876\u697c\u3002<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ Recursion + Memorization \u8bb0\u5fc6\u5316\u9012\u5f52<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {\r\n        vector&lt;int&gt; m(cost.size() + 1);\r\n        return dp(cost, m, cost.size());\r\n    }\r\nprivate:\r\n    \/\/ min cost to climb to i-th step\r\n    int dp(vector&lt;int&gt;&amp; cost, vector&lt;int&gt;&amp; m, int i) {\r\n        if (i &lt;= 1) return 0;        \r\n        if (m[i] &gt; 0) return m[i];\r\n        return m[i] = min(dp(cost, m, i - 1) + cost[i - 1], \r\n                          dp(cost, m, i - 2) + cost[i - 2]);\r\n    }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {\r\n        vector&lt;int&gt; m(cost.size() + 1);\r\n        return min(dp(cost, m, cost.size() - 1),\r\n                   dp(cost, m, cost.size() - 2));\r\n    }\r\nprivate:\r\n    \/\/ min cost before leaving i-th step\r\n    int dp(vector&lt;int&gt;&amp; cost, vector&lt;int&gt;&amp; m, int i) {\r\n        if (i &lt; 0) return 0;        \r\n        if (m[i] &gt; 0) return m[i];\r\n        return m[i] = min(dp(cost, m, i - 1), dp(cost, m, i - 2)) + cost[i];\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ DP \u52a8\u6001\u89c4\u5212<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {\r\n        const int n = cost.size();\r\n        vector&lt;int&gt; dp(n, INT_MAX); \/\/ d[i] min cost before leaving i\r\n        dp[0] = cost[0];\r\n        dp[1] = cost[1];\r\n        for (int i = 2; i &lt; n; ++i)\r\n            dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\r\n        \/\/ We can reach top from either n - 1, or n - 2\r\n        return min(dp[n - 1], dp[n - 2]);\r\n    }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 12 ms\r\nclass Solution {\r\npublic:\r\n    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {\r\n        \/\/ dp[i] : min cost to climb to n-th step\r\n        vector&lt;int&gt; dp(cost.size() + 1, 0);\r\n        for (int i = 2; i &lt;= cost.size(); ++i)\r\n            dp[i] = min(dp[i - 1] + cost[i - 1],\r\n                        dp[i - 2] + cost[i - 2]);\r\n        return dp[cost.size()];\r\n    }\r\n};<\/pre>\n<p>O(1) Space<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 15 ms\r\nclass Solution {\r\npublic:\r\n    int minCostClimbingStairs(vector&lt;int&gt;&amp; cost) {        \r\n        int dp1 = 0;\r\n        int dp2 = 0;\r\n        for (int i = 2; i &lt;= cost.size(); ++i) {\r\n            int dp = min(dp1 + cost[i - 1], dp2 + cost[i - 2]);\r\n            dp2 = dp1;\r\n            dp1 = dp;\r\n        }\r\n        return dp1;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: On a staircase, the\u00a0i-th step has some non-negative cost\u00a0cost[i]\u00a0assigned (0 indexed). Once you pay the cost, you can either climb one or two steps.&#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":[18,222,191],"class_list":["post-1271","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-easy","tag-stairs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1271","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=1271"}],"version-history":[{"count":11,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1271\/revisions"}],"predecessor-version":[{"id":1293,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1271\/revisions\/1293"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}