【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.

【思路】

求从左上角到右下角的最小路径值,典型的动态规划。

动规方程:dp[i][j] = grid[i][j] + min( dp[i-1][j], dp[i][j-1] );

时间复杂度为O(m*n),因为至少要遍历一遍二维表。

空间复杂度,如果用二维数组来存动规过程,那么就是O(m*n);但如果用一维数组来存动规过程,空间复杂度就为O(n)。

【Java代码】

public class Solution {
    // DP with two dimension table
    public int minPathSum1(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        int[][] ans = new int[m][n];
        ans[0][0] = grid[0][0];
        for (int i = 1; i < m; i++) {
            ans[i][0] = ans[i-1][0] + grid[i][0];
        }
        for (int j = 1; j < n; j++) {
            ans[0][j] = ans[0][j-1] + grid[0][j];
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                ans[i][j] = Math.min(ans[i-1][j], ans[i][j-1]) + grid[i][j];
            }
        }

        return ans[m-1][n-1];
    }

    // DP with one dimension table
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        int[] ans = new int[n];
        ans[0] = grid[0][0];
        for (int j = 1; j < n; j++) {
            ans[j] = ans[j-1] + grid[0][j];
        }

        for (int i = 1; i < m; i++) {
            ans[0] += grid[i][0];
            for (int j = 1; j < n; j++) {
                ans[j] = Math.min(ans[j-1], ans[j]) + grid[i][j];
            }
        }

        return ans[n-1];
    }
}

这道题非常典型,之前好像练过一道类似的,所以这次写的时候非常顺手。

时间: 2024-08-03 23:34:30

【LeetCode】Minimum Path Sum 解题报告的相关文章

LeetCode: Minimum Path Sum 解题报告

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: 相当基础

Leetcode:Minimum Path Sum 矩形网格最小路径和

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. 解题分析: 每次只能向下或者向

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/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 r

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

LeetCode: Path Sum 解题报告

Path Sum 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 and sum = 22,              5             / \        

LeetCode Oj 112. Path Sum 解题报告

112. Path Sum My Submissions Question Total Accepted: 91133 Total Submissions: 295432 Difficulty: Easy 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

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

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. 首先每一个路径的上一个路径都是来自于其上方和左方 现将最上面的路径进行求和,最左边的路径进行求和