[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

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.

题目很容易读懂,就是找到一条路径从左上到右下并且使得路径上的所有值加起来最小。

思路就是用一个二维数组记录最小的值,每次比较之后相加本身。

public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] val = new int[m][n];
        val[0][0] = grid[0][0];
        for (int i = 1; i < m; i++)
            val[i][0] = grid[i][0] + val[i - 1][0];
        for (int i = 1; i < n; i++)
            val[0][i] = grid[0][i] + val[0][i - 1];
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                int tmp = val[i - 1][j] >= val[i][j - 1] ? val[i][j - 1]
                        : val[i - 1][j];
                val[i][j] = grid[i][j] + tmp;
            }
        }
        return val[m - 1][n - 1];
    }

另外一种就是一维数组的方法,其实跟这个是一样的,就是少用一点内存,但是没有上面的好理解。

/**
     * 一维数组的方法
     * @param grid
     * @return
     */
    public int minPathSum1(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[] val = new int[n];
        val[0] = grid[0][0];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i > 0 && j > 0) {
                    int tmp = val[j] >= val[j - 1] ? val[j - 1] : val[j];
                    val[j] = grid[i][j] + tmp;
                } else if (j > 0) {
                    //处理第一行
                    val[j] = grid[i][j] + val[j - 1];
                }
                else if (i > 0) {
                    //处理第一列
                    val[0] = grid[i][j] + val[0];
                }
            }
        }
        return val[n - 1];
    }
目录
相关文章
|
6月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
25 0
LeetCode 433. Minimum Genetic Mutation
现在给定3个参数 — start, end, bank,分别代表起始基因序列,目标基因序列及基因库,请找出能够使起始基因序列变化为目标基因序列所需的最少变化次数。如果无法实现目标变化,请返回 -1。
48 0
LeetCode 433. Minimum Genetic Mutation
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
45 0
LeetCode 329. Longest Increasing Path in a Matrix
|
存储 索引
LeetCode 310. Minimum Height Trees
对于一个具有树特征的无向图,我们可选择任何一个节点作为根。图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树。给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点。
51 0
LeetCode 310. Minimum Height Trees
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
88 0
LeetCode 209. Minimum Size Subarray Sum
LeetCode 111. Minimum Depth of Binary Tree
给定一棵二叉树,返回其最短深度. 题目要求是返回一颗二叉树从根节点到到所有叶节点中,深度最小的值.
60 0
LeetCode 111. Minimum Depth of Binary Tree
|
Unix Python
LeetCode 71. Simplify Path
给定文件的绝对路径(Unix下的路径)字符串,简化此字符串。
64 0
LeetCode 71. Simplify Path
LeetCode 64. Minimum Path Sum
给定m x n网格填充非负数,找到从左上到右下的路径,这最小化了沿其路径的所有数字的总和。 注意:您只能在任何时间点向下或向右移动。
69 0
LeetCode 64. Minimum Path Sum
|
4天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
7 0
|
4天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
8 0