{"id":3720,"date":"2018-08-27T08:02:07","date_gmt":"2018-08-27T15:02:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3720"},"modified":"2018-08-30T09:25:41","modified_gmt":"2018-08-30T16:25:41","slug":"leetcode-120-triangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-120-triangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 120. Triangle"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 120. Triangle - \u5237\u9898\u627e\u5de5\u4f5c EP24\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ITV2CglqkWU?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><strong>Problem<\/strong><\/h1>\n<p>Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.<\/p>\n<p>For example, given the following triangle<\/p>\n<pre class=\"crayon:false\">[\r\n     [<strong>2<\/strong>],\r\n    [<strong>3<\/strong>,4],\r\n   [6,<strong>5<\/strong>,7],\r\n  [4,<strong>1<\/strong>,8,3]\r\n]\r\n<\/pre>\n<p>The minimum path sum from top to bottom is\u00a0<code>11<\/code>\u00a0(i.e.,\u00a0<strong>2<\/strong>\u00a0+\u00a0<strong>3<\/strong>\u00a0+\u00a0<strong>5<\/strong>\u00a0+\u00a0<strong>1<\/strong>\u00a0= 11).<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>Bonus point if you are able to do this using only\u00a0<em>O<\/em>(<em>n<\/em>) extra space, where\u00a0<em>n<\/em>\u00a0is the total number of rows in the triangle.<\/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^2)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  \/\/ [[2]                 [[2]\r\n  \/\/  [3, 4]]              [5, 6]\r\n  \/\/  [6, 5, 7]]           [11, 10, 11]\r\n  \/\/  [4, 1, 8, 3]]        [15, 11, 18, 14]]\r\n  int minimumTotal(vector&lt;vector&lt;int&gt;&gt;&amp; t) {\r\n    int n = t.size();        \r\n\r\n    \/\/ t[i][j] := minTotalOf(i,j)\r\n    \/\/ t[i][j] += min(t[i - 1][j], t[i - 1][j - 1])\r\n\r\n    for (int i = 0; i &lt; n; ++i)\r\n      for (int j = 0; j &lt;= i; ++j) {\r\n        if (i == 0 &amp;&amp; j == 0) continue;\r\n        if (j == 0) t[i][j] += t[i - 1][j];\r\n        else if (j == i) t[i][j] += t[i - 1][j - 1];\r\n        else t[i][j] += min(t[i - 1][j], t[i - 1][j - 1]);\r\n      }\r\n\r\n    return *std::min_element(begin(t.back()), end(t.back()));\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.&#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,177],"class_list":["post-3720","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3720","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=3720"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3720\/revisions"}],"predecessor-version":[{"id":3754,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3720\/revisions\/3754"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}