{"id":9751,"date":"2022-06-14T20:57:20","date_gmt":"2022-06-15T03:57:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9751"},"modified":"2022-06-14T21:10:39","modified_gmt":"2022-06-15T04:10:39","slug":"leetcode-2304-minimum-path-cost-in-a-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2304-minimum-path-cost-in-a-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2304. Minimum Path Cost in a Grid"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;<code>m x n<\/code>&nbsp;integer matrix&nbsp;<code>grid<\/code>&nbsp;consisting of&nbsp;<strong>distinct<\/strong>&nbsp;integers from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>m * n - 1<\/code>. You can move in this matrix from a cell to any other cell in the&nbsp;<strong>next<\/strong>&nbsp;row. That is, if you are in cell&nbsp;<code>(x, y)<\/code>&nbsp;such that&nbsp;<code>x &lt; m - 1<\/code>, you can move to any of the cells&nbsp;<code>(x + 1, 0)<\/code>,&nbsp;<code>(x + 1, 1)<\/code>, &#8230;,&nbsp;<code>(x + 1, n - 1)<\/code>.&nbsp;<strong>Note<\/strong>&nbsp;that it is not possible to move from cells in the last row.<\/p>\n\n\n\n<p>Each possible move has a cost given by a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D array&nbsp;<code>moveCost<\/code>&nbsp;of size&nbsp;<code>(m * n) x n<\/code>, where&nbsp;<code>moveCost[i][j]<\/code>&nbsp;is the cost of moving from a cell with value&nbsp;<code>i<\/code>&nbsp;to a cell in column&nbsp;<code>j<\/code>&nbsp;of the next row. The cost of moving from cells in the last row of&nbsp;<code>grid<\/code>&nbsp;can be ignored.<\/p>\n\n\n\n<p>The cost of a path in&nbsp;<code>grid<\/code>&nbsp;is the&nbsp;<strong>sum<\/strong>&nbsp;of all values of cells visited plus the&nbsp;<strong>sum<\/strong>&nbsp;of costs of all the moves made. Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;cost of a path that starts from any cell in the&nbsp;<strong>first<\/strong>&nbsp;row and ends at any cell in the&nbsp;<strong>last<\/strong>&nbsp;row.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/04\/28\/griddrawio-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]\n<strong>Output:<\/strong> 17\n<strong>Explanation: <\/strong>The path with the minimum possible cost is the path 5 -&gt; 0 -&gt; 1.\n- The sum of the values of cells visited is 5 + 0 + 1 = 6.\n- The cost of moving from 5 to 0 is 3.\n- The cost of moving from 0 to 1 is 8.\nSo the total cost of the path is 6 + 3 + 8 = 17.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The path with the minimum possible cost is the path 2 -&gt; 3.\n- The sum of the values of cells visited is 2 + 3 = 5.\n- The cost of moving from 2 to 3 is 1.\nSo the total cost of this path is 5 + 1 = 6.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>2 &lt;= m, n &lt;= 50<\/code><\/li><li><code>grid<\/code>&nbsp;consists of distinct integers from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>m * n - 1<\/code>.<\/li><li><code>moveCost.length == m * n<\/code><\/li><li><code>moveCost[i].length == n<\/code><\/li><li><code>1 &lt;= moveCost[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Let dp[i][j] := min cost to reach grid[i][j] from the first row.<\/p>\n\n\n\n<p>dp[i][j] = min{grid[i][j] + dp[i &#8211; 1][k] + moveCost[grid[i &#8211; 1][k]][j]}  0 &lt;= k &lt; n<br><\/p>\n\n\n\n<p>For each node, try all possible nodes from the previous row.<\/p>\n\n\n\n<p>Time complexity: O(m*n<sup>2<\/sup>)<br>Space complexity: O(m*n) -&gt; O(n)<\/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 minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {\n    const int m = grid.size();\n    const int n = grid[0].size();\n    vector<vector<int>> dp(m, vector<int>(n, INT_MAX));    \n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        for (int k = 0; k < n; ++k)          \n          dp[i][j] = min(dp[i][j], grid[i][j] + \n                         (i ? dp[i - 1][k] + moveCost[grid[i - 1][k]][j] : 0));\n    return *min_element(begin(dp[m - 1]), end(dp[m - 1]));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;m x n&nbsp;integer matrix&nbsp;grid&nbsp;consisting of&nbsp;distinct&nbsp;integers from&nbsp;0&nbsp;to&nbsp;m * n &#8211; 1. You can move in this matrix from a cell to any other&#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],"class_list":["post-9751","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9751","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=9751"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9751\/revisions"}],"predecessor-version":[{"id":9755,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9751\/revisions\/9755"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}