`

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.

最小路径求和的问题,用动态规划来解决。每经过一个点都记录从开始到这个点的最小带权路径。当i = 0 或者j = 0的时候,因为只有一个方向可以走,这时的递推式为grid[i][0] = grid[i - 1][0] + grid[i][0] (j = 0) 和grid[0][j] = grid[0][j - 1] + grid[0][j] ( i = 0)。当i >0 并且j > 0 时,递推式为grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j],每次都取一个和最小的路径。代码如下:
public class Solution {
    public int minPathSum(int[][] grid) {
        if(grid == null || grid.length == 0) return 0;
        int m = grid.length;
        int n = grid[0].length;
        for(int i = 1; i < m; i++)
            grid[i][0] = grid[i - 1][0] + grid[i][0];
        for(int j = 1; j < n; j++)
            grid[0][j] = grid[0][j - 1] + grid[0][j];
        for(int i = 1; i < m; i++)
            for(int j = 1; j < n; j++) {
                grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j];
            }
        return grid[m - 1][n - 1];
    }
}
分享到:
评论

相关推荐

    cpp-算法精粹

    Minimum Path Sum Edit Distance Decode Ways Distinct Subsequences Word Break Word Break II Dungeon Game House Robber House Robber II House Robber III Range Sum Query - Immutable Range Sum Query 2D - ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number ...Sum ...Sum ...Sum ...Minimum Path Sum 73

    leetcode分类-LeetCode:力码

    Minimum Path Sum Unique Paths Unique Paths II Longest Palindromic Substring Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    64.Minimum Path Sum 2017/03/09 70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 (Lintcode)92.Backpack, (Lintcode)125....

    进阶班7、8节作业1

    进阶班七八节作业:矩阵的最小路径和:https://leetcode.com/problems/minimum-path-sum/description/最长递

    Anfany#LeetCode_Python3_Solution#64 最小路径和1

    二、Python3程序64_Minimum_Path_Sum 最小路径和# 利用动态规划的方法# 要到达索引为[r,l]的网格# 一是从[r,l-1]的网格向右

    算法上机!!

    (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm ...

    64. 最小路径和

    链接:https://leetcode-cn.com/problems/minimum-path-sum 思路 这个题目和120. 三角形最小路径和很像,解法是类似的。 所以这里直接通过自底向上动态规划的方式来求解。沿着移动方向相反的方向,即从右下角开始...

    基于dijkstra的低耗路由matlab仿真

    [costs] is an LxM matrix of minimum cost values for the minimal paths [paths] is an LxM cell containing the shortest path arrays [showWaitbar] (optional) a scalar logical that initializes a waitbar ...

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    The Algorithm Design Manual (2rd Edition)

    15.4 Shortest Path 15.5 Transitive Closure and Reduction 15.6 Matching 15.7 Eulerian Cycle/Chinese Postman 15.8 Edge and Vertex Connectivity 15.9 Network Flow 15.10 Drawing Graphs Nicely 15.11...

    Introduction to Algorithms, 3rd edtion

    21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 22.3 ...

    算法导论 第三版 英文原版 高清文字版

    21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 22.3 ...

    算法导论-introduction to algrithms 3rd edition

    21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 ...

    全面的算法代码库

    ISAP算法 Improved-Shortest-Augmenting-Path(Naive) 插入排序 Insertion-Sort 字符串匹配(KMP) Knuth-Morris-Pratt 最小生成树(Kruskal) Kruskal 最近公共祖先(Tarjan) Least-Common-Ancestor(Tarjan) ...

    算法导论英文版

    2l.4 Analysis of union by rank with path compression 50 VI Graph Algorithms Introduction 525 22 Elementary Graph Algorithms 527 22.l Representations of graphs 527 22.2 Breadth-first search 531 22.3 ...

    算法导论--Introduction.to.Algorithms

    21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 22.3 ...

    kgb档案压缩console版+源码

    has the minimum number of digits (bytes) needed to satisfy (1). Such coding is within 1 byte of the Shannon limit, log 1/P(y), so compression depends almost entirely on the goodness of the model, P, i...

    计算机网络第六版答案

    Computer Networking: A Top-Down Approach, 6th Edition Solutions to Review Questions and Problems Version Date: May 2012 ...This document contains the solutions to review questions and problems for...

Global site tag (gtag.js) - Google Analytics