Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode <dp>64. Minimum Path Sum

LeetCode <dp>64. Minimum Path Sum

原创
作者头像
大学里的混子
修改于 2018-11-15 06:59:33
修改于 2018-11-15 06:59:33
69800
代码可运行
举报
文章被收录于专栏:LeetCodeLeetCode
运行总次数:0
代码可运行

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.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 13111 minimizes the sum.

题目大意:求矩阵中的最小的路径

思路:这是一个典型的动态规划问题

解法一:

本解法的空间复杂度为O(m*n).

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    int sum = 0;
    int [][] sss ;
    public int minPathSum(int[][] grid) {
        if (grid == null || grid.length ==0) return 0;
        sss = new int[grid.length][grid[0].length];
        return minPathSum_recursion(grid,0,0);
    }

 
    public int minPathSum_recursion(int[][] grid,int m ,int n) {
        if (m==grid.length-1) {
            int a = 0;
            for (int i = n;i<grid[0].length;i++){
                a = a + grid[m][i];
            }
            return a;
        }

        if (n == grid[0].length-1) {
            int b = 0;
            for (int i = m;i<grid.length;i++){
                b = b + grid[i][n];
            }
            return b;
        }
        if (sss[m][n] == 0){
            sss[m][n] = sum + grid[m][n] + Math.min(minPathSum_recursion(grid,m+1,n),minPathSum_recursion(grid,m,n+1));
        }
        return sss[m][n];
    }

解法二:

本解法的空间复杂度是min{m,n}.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public int minPathSum(int[][] grid) {
        if (grid == null ||grid.length ==0|| grid[0].length ==0) return 0;
        int min = Math.min(grid.length,grid[0].length);
        int max = Math.max(grid.length,grid[0].length);
        int [] arr = new int[min];
        boolean rowlonger = grid.length == max; // 行数大于列数
        arr[0] = grid[0][0];
        for (int i = 1;i<min;i++){
            arr[i] = arr[i-1] + (rowlonger?grid[0][i]:grid[i][0]);
        }

        for (int i=1;i<max;i++){
            arr[0] = arr[0]+(rowlonger?grid[i][0]:grid[0][i]);
            for (int j = 1; j<min;j++){
                arr[j] = Math.min(arr[j-1],arr[j])+(rowlonger?grid[i][j]:grid[j][i]);
            }
        }
        return arr[min-1];
    }

这种压缩的方法适用于只求结果不求过程的题目,如果要求输出路径,那么还是需要使用解法一的。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【DP】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.
echobingo
2019/06/13
3720
leetcode: 64. Minimum Path Sum
Problem # 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. # # Example 1: #
JNingWei
2018/09/27
3580
Dynamic Programming - 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.
ppxai
2020/09/23
3340
LeetCode 0064 - 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.
Reck Zhang
2021/08/11
2100
[LeetCode] 64. Minimum Path Sum
Minimum Path Sum是LeetCode网站上的一个题目,题目描述为给定一个m x n的二维整数数组grid,从左上角到右下角的最小路径和是多少?路径只能向右或向下移动。
用户1148830
2018/01/03
4960
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.
卡尔曼和玻尔兹曼谁曼
2019/01/22
3620
LeetCode 64. 最小路径和
https://leetcode-cn.com/problems/minimum-path-sum/
freesan44
2021/11/14
2730
LeetCode 64. 最小路径和
​LeetCode刷题实战64:最小路径和
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/01/20
2690
​LeetCode刷题实战64:最小路径和
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.
ruochen
2022/02/16
1.2K0
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.
ruochen
2022/01/09
1.3K0
【Leetcode】64. 最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
Leetcode名企之路
2018/10/09
9570
【Leetcode】64. 最小路径和
LeetCode64. 最小路径和
思路:动态规划 只关注左边和上边 //给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 // // 说明:每次只能向下或者向右移动一步。 // // // // 示例 1: // // //输入:grid = [[1,3,1],[1,5,1],[4,2,1]] //输出:7 //解释:因为路径 1→3→1→1→1 的总和最小。 // // // 示例 2: // // //输入:grid = [[1,2,3],[4,5,6]] //输出:1
周杰伦本人
2022/10/25
2230
LC64—最小路径和
给定一个包含非负整数的 *m* x *n* 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
Java架构师必看
2021/05/14
2730
LC64—最小路径和
LeetCode 0064. 最小路径和[动态规划详解]
给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
Yano_nankai
2021/03/05
7970
LeetCode 0064. 最小路径和[动态规划详解]
【动态规划/路径问题】进阶「最小路径和」问题 ...
给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
宫水三叶的刷题日记
2021/03/12
2.1K0
一天一大 leet(最小路径和)难度:中等-Day20200723
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
前端小书童
2020/09/24
2170
一天一大 leet(最小路径和)难度:中等-Day20200723
LeetCode 动态规划 题目分类汇总
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
Yano_nankai
2018/10/08
1.3K0
LeetCode 动态规划 题目分类汇总
Leetcode 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. 和62,63如出一辙的套路,没什么含量 class Solution
triplebee
2018/01/12
5540
☆打卡算法☆LeetCode 64、最小路径和 算法解析
链接:64. 最小路径和 - 力扣(LeetCode) (leetcode-cn.com)
恬静的小魔龙
2022/08/07
3040
☆打卡算法☆LeetCode 64、最小路径和  算法解析
LeetCode <dp>120&931 Triangle&Minimum Falling Path Sum
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
大学里的混子
2018/11/15
3860
相关推荐
【DP】64. Minimum Path Sum
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验