{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "96ce3e837e5c4135b4375ce9e8b29754",
  "keywords": "数组,动态规划,矩阵",
  "title": "最小路径和",
  "desc": [
    {
      "content": "\n<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。</p><p><strong>说明：</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入：</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出：</strong>7<strong><br />解释：</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出：</strong>12</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>m == grid.length</code></li>\t<li><code>n == grid[i].length</code></li>\t<li><code>1 <= m, n <= 200</code></li>\t<li><code>0 <= grid[i][j] <= 100</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int minPathSum(int[][] grid) {\n\t\tint m = grid.length;\n\t\tint n = grid[0].length;\n\n\t\tint sum = 0;\n\n\t\tif (m < 1 || n < 1)\n\t\t\treturn 0;\n\t\tif (m == 1) {\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\tsum = sum + grid[0][i];\n\t\t\t}\n\t\t\treturn sum;\n\t\t}\n\t\tif (n == 1) {\n\t\t\tfor (int i = 0; i < m; i++) {\n\t\t\t\tsum = sum + grid[i][0];\n\t\t\t}\n\t\t\treturn sum;\n\t\t}\n\n\t\tint[][] dp = new int[m][n];\n\t\tdp[0][0] = grid[0][0];\n\n\t\tfor (int k = 1; k < m; k++) {\n\t\t\tdp[k][0] = grid[k][0] + dp[k - 1][0];\n\t\t}\n\n\t\tfor (int l = 1; l < n; l++) {\n\t\t\tdp[0][l] = grid[0][l] + dp[0][l - 1];\n\t\t}\n\n\t\tfor (int k = 1; k < m; k++) {\n\t\t\tfor (int l = 1; l < n; l++) {\n\t\t\t\tdp[k][l] = grid[k][l] + Math.min(dp[k - 1][l], dp[k][l - 1]);\n\t\t\t}\n\t\t}\n\t\treturn dp[m - 1][n - 1];\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-40c4c366546a44b5930491db827d2c14",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470227"
}