{"id":5963,"date":"2019-12-14T14:04:07","date_gmt":"2019-12-14T22:04:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5963"},"modified":"2019-12-14T14:04:19","modified_gmt":"2019-12-14T22:04:19","slug":"leetcode-1289-minimum-falling-path-sum-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1289-minimum-falling-path-sum-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1289. Minimum Falling Path Sum II"},"content":{"rendered":"\n<p>Given a square grid&nbsp;of integers&nbsp;<code>arr<\/code>, a&nbsp;<em>falling path with non-zero shifts<\/em>&nbsp;is a choice of&nbsp;exactly one element from each row of&nbsp;<code>arr<\/code>, such that no two elements chosen in adjacent rows are in&nbsp;the same column.<\/p>\n\n\n\n<p>Return the&nbsp;minimum&nbsp;sum of a falling path with non-zero shifts.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [[1,2,3],[4,5,6],[7,8,9]]\n<strong>Output:<\/strong> 13\n<strong>Explanation: <\/strong>\nThe possible falling paths are:\n[1,5,9], [1,5,7], [1,6,7], [1,6,8],\n[2,4,8], [2,4,9], [2,6,7], [2,6,8],\n[3,4,8], [3,4,9], [3,5,7], [3,5,9]\nThe falling path with the smallest sum is&nbsp;[1,5,7], so the answer is&nbsp;13.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length == arr[i].length &lt;= 200<\/code><\/li><li><code>-99 &lt;= arr[i][j] &lt;= 99<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i][j] = min(dp[i-1][k]), 1 &lt;= k &lt;= m, k != j<\/p>\n\n\n\n<p>Time complexity: O(n^3)<br>Space complexity: O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minFallingPathSum(vector<vector<int>>& arr) {\n    constexpr int kInf = 1e6;\n    for (int i = 1; i < arr.size(); ++i)\n      for (int j = 0; j < arr[0].size(); ++j) {\n        int m = kInf;\n        for (int k = 0; k < arr[0].size(); ++k) {\n          if (k == j) continue;\n          m = min(m, arr[i - 1][k]);\n        }\n        arr[i][j] += m;\n    }\n    return *min_element(begin(arr.back()), end(arr.back()));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a square grid&nbsp;of integers&nbsp;arr, a&nbsp;falling path with non-zero shifts&nbsp;is a choice of&nbsp;exactly one element from each row of&nbsp;arr, such that no two elements chosen&#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,217],"class_list":["post-5963","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5963","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=5963"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5963\/revisions"}],"predecessor-version":[{"id":5965,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5963\/revisions\/5965"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}