{"id":3725,"date":"2018-08-27T08:17:13","date_gmt":"2018-08-27T15:17:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3725"},"modified":"2018-08-30T09:11:17","modified_gmt":"2018-08-30T16:11:17","slug":"leetcode-70-climbing-stairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-70-climbing-stairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 70. Climbing Stairs"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 70.  Climbing Stairs - \u5237\u9898\u627e\u5de5\u4f5c EP21\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/LpsDWvkd-4Q?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<h1>Problem<\/h1>\n<p>You are climbing a stair case. It takes\u00a0<em>n<\/em>\u00a0steps to reach to the top.<\/p>\n<p>Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?<\/p>\n<p><strong>Note:<\/strong>\u00a0Given\u00a0<em>n<\/em>\u00a0will be a positive integer.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 2\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation:<\/strong> There are two ways to climb to the top.\r\n1. 1 step + 1 step\r\n2. 2 steps\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 3\r\n<strong>Output:<\/strong> 3\r\n<strong>Explanation:<\/strong> There are three ways to climb to the top.\r\n1. 1 step + 1 step + 1 step\r\n2. 1 step + 2 steps\r\n3. 2 steps + 1 step<\/pre>\n<p>&nbsp;<\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++ O(n)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  int climbStairs(int n) {\r\n    \/\/ f[i] = climbStairs(i)\r\n    vector&lt;int&gt; f(n + 1, 0);\r\n    f[0] = f[1] = 1;\r\n    \/\/ f[i] = f[i-1] + f[i-2]\r\n    for (int i = 2;i &lt;= n; ++i)\r\n      f[i] = f[i - 1] + f[i - 2];\r\n    return f[n];\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ O(1)<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  int climbStairs(int n) {\r\n    int two = 1;\r\n    int one = 1;\r\n    int curr = 1;\r\n    \/\/ curr = two + one\r\n    for (int i = 2; i &lt;= n; ++i) {\r\n        curr = two + one;\r\n        two = one;\r\n        one = curr;\r\n    }\r\n    return curr;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem You are climbing a stair case. It takes\u00a0n\u00a0steps to reach to the top. Each time you can either climb 1 or 2 steps. In&#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],"class_list":["post-3725","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3725","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=3725"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions"}],"predecessor-version":[{"id":3752,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3725\/revisions\/3752"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}