【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

【064-Minimum Path Sum(最小路径和)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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 x n的方格,每一个元素的值都是非负的。找出从左上角顶点,到右下角顶点和的值最小的路径,返回找到的最小和。

解题思路

   分治法,

  第一个: S[0][0] = grid[0][0]

  第一行: S[0][j] = S[0][j - 1] + grid[0][j]

  第一列: S[i][0] = S[i - 1][0] + grid[i][0]

  其他情况:S[i][j] = min(S[i - 1][j], S[i][j - 1]) + grid[i][j]

代码实现

算法实现类

public class Solution {

    public int minPathSum(int[][] grid) {
        // 參数检验
        if (grid == null || grid.length < 1 || grid[0].length < 1) {
            return 0;
        }

        int[][] result = new int[grid.length][grid[0].length];
        // 第一个
        result[0][0] = grid[0][0];

        // 第一行
        for (int i = 1; i < result[0].length; i++) {
            result[0][i] = result[0][i - 1] + grid[0][i];
        }

        // 第一列
        for (int i = 1; i < result.length; i++) {
            result[i][0] = result[i - 1][0] + grid[i][0];
        }

        // 其他情况
        for (int i = 1; i < result.length; i++) {
            for (int j = 1; j < result[0].length; j++) {
                result[i][j] = Math.min(result[i - 1][j], result[i][j - 1]) + grid[i][j];
            }
        }

        return result[result.length - 1][result[0].length - 1];
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    // 动态归划和分枝限界,以下的方法会超时
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public int minPathSum2(int[][] grid) {
        // 參数检验
        if (grid == null || grid.length < 1 || grid[0].length < 1) {
            return 0;
        }

        // 用于记录最小的路径各
        int[] minSum = {Integer.MAX_VALUE};
        int[] curSum = {0};
        // 解题
        solve(grid, 0, 0, curSum, minSum);

        // 返回结果
        return minSum[0];
    }

    public void solve(int[][] grid, int row, int col, int[] curSum, int[] minSum) {
        // 假设已经到达终点
        if (row == grid.length - 1 && col == grid[0].length - 1) {
            curSum[0] += grid[row][col];

            // 更新最小的和
            if (curSum[0] < minSum[0]) {
                minSum[0] = curSum[0];
            }

            curSum[0] -= grid[row][col];
        }
        // 还未到达终点,而且在网格内
        else if (row >= 0 && row < grid.length && col >= 0 && col < grid[0].length) {
            curSum[0] += grid[row][col];
            // 当前的和仅仅有不小于记录到的最小路径值才干进行下一步操作
            if (curSum[0] <= minSum[0]) {
                // 向右走
                solve(grid, row, col + 1, curSum, minSum);
                // 向下走
                solve(grid, row + 1, col, curSum, minSum);
            }
            curSum[0] -= grid[row][col];
        }
    }
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47203311

时间: 2024-12-25 07:17:14

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】的相关文章

【LeetCode-面试算法经典-Java实现】【112-Path Sum(路径和)】

[112-Path Sum(路径和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree an

lintcode 容易题:Minimum Path Sum 最小路径和

题目: 最小路径和 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径. 样例 注意 你在同一时间只能向下或者向右移动一步 解题: 这个和求三角形的最小路径的差不多,这里是个矩阵,第一列和第一行要单独处理,每一点的值等于自身的值加上上一点的值,对于中间节点:grid[i][j] + = min( grid[i-1][j] , grid[i][j-1]) ,也就是说,i j 点的值只能是其左侧的点或者上侧的点加过来,这个时间复杂度比较高O(N2) Java程序: p

Leetcode: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. 此题解法同Triangle 数字三角形.在此不再赘述. class

064 Minimum Path Sum

064 Minimum Path Sum 纯dp class Solution: # @param {integer[][]} grid # @return {integer} def minPathSum(self, grid): m = len(grid) if m == 0: return 0 n = len(grid[0]) for i in range(1, m): grid[i][0] += grid[i-1][0] for j in range(1, n): grid[0][j]

【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】

[113-Path Sum II(路径和II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2

【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) m

【LeetCode-面试算法经典-Java实现】【016-3 Sum Closest(最接近的三个数的和)】

[016-3 Sum Closest(最接近的三个数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input

【LeetCode-面试算法经典-Java实现】【216-Combination Sum III (组合数的和)】

[216-Combination Sum III (组合数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination

Java for LeetCode 064 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. 解题思路: dp问题,和上一题一样,JAVA实现如下: stati

【LeetCode】064. 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. 题解: Solution 1 () class Solut