64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
题意:
给定一个充满非负数的m×n网格,从左上角到右下角找到一条路径,它沿着路径将所有数的总和最小化。
思路:
方法一:
思路同62. Unique Paths利用动态规划,但是此题在通过动态转移方程计算的时候,就是每前进一步,就计算上一步的最小值,根据之前的最小值推断出当前最小值,即局部最优构造全局最优。但是网上的好处就是没有改变原数组的值,利用新的数组来存储最小值。
1 | class Solution { |
方法二:
思路也是动态规划,但是与方法一不同点时好处就是利用新的一维数组来存储要求元素点从上边到来的路径值,没有改变原数组的值。
1 | class Solution { |
Java Code
1 | class Solution { |