/**
* Source : 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 right at any point in time.
*
*/
public class MinimumPathSum { /**
* 求到右下角所有路径中最小的和
*
* 依然使用动态规划
* grid[i][j] += min (grif[i][j-1] + grid[]i-1[j])
* 如果是第一列则
* grid[i][j] += grid[i-1][j]
* 如果是第一行则
* grid[i][j] += grid[i][j+1]
*
* @param grid
* @return
*/
public int findminimumPathSum (int[][] grid) {
if (grid.length <= 0 || grid[0].length <= 0) {
return 0;
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (i == 0 && j == 0) {
continue;
}
if (i == 0) {
grid[i][j] += grid[i][j-1];
} else if (j == 0) {
grid[i][j] += grid[i-1][j];
} else {
grid[i][j] += Math.min(grid[i][j-1], grid[i-1][j]);
}
}
}
return grid[grid.length-1][grid[0].length-1];
} public static void main(String[] args) {
MinimumPathSum minimumPathSum = new MinimumPathSum();
int[][] arr = new int[][]{
{1,2,3,4},
{3,5,3,4},
{3,1,3,8}
}; System.out.println(minimumPathSum.findminimumPathSum(arr));
}
}

leetcode — minimum-path-sum的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  2. 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 b ...

  3. [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 ...

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

  5. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  6. LeetCode:Minimum Path Sum(网格最大路径和)

    题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  7. LeetCode Minimum Path Sum (简单DP)

    题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...

  8. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  10. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. ubuntu18.04搭建nfs

    1.服务端安装 #apt-get update -y #apt-get install -y nfs-kernel-server #apt-get enable nfs-kernel-server 2 ...

  2. python 实现rsa 的加密解密存读取(PEM格式证书)【转发】

    来源:CSDN 原文:https://blog.csdn.net/sjt1996/article/details/83377800

  3. xml文档格式学习笔记

    xml入门经典 (pdf书籍) https://www.cnblogs.com/zhaopengcheng/p/6848802.html

  4. 一. IntelliJ IDEA详细配置文档之初始环境搭建

    前言 对于用惯了eclipse的同学来说, 突然切换为idea不是一件那么容易的事情, 所以我会发布一系列只讲解idea使用技巧的文章, 请大家多多关注.  本系列文章的配置参考网上某教程的讲解, 本 ...

  5. [转]构建高性能MySQL体系

    来源:http://www.yunweipai.com/archives/21232.html 构建高性能MySQL系统涵盖从单机.硬件.OS.文件系统.内存到MySQL 本身的配置,以及schema ...

  6. vim配置文件.vimrc

    20171127备份 syntax on "自动语法高亮 set number "显示行号 set autoindent "回车后自动缩进 set tabstop=4 & ...

  7. 逻辑回归 vs 决策树 vs 支持向量机(II)

    原文地址: Logistic Regression vs Decision Trees vs SVM: Part II 在这篇文章,我们将讨论如何在逻辑回归.决策树和SVM之间做出最佳选择.其实 第一 ...

  8. 默认空间和webapps下项目部署

    用eclipse默认的工作区和webapps的细节 用{WORKSPACE}表示你的eclipse的工作空间根目录,然后你打开 {WORKSPACE}\.metadata\.plugins\org.e ...

  9. XSS攻击 CSRF攻击

    XSS攻击: 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆, 故将跨站脚本攻击缩写为XSS.恶意攻击者 ...

  10. 关于Podfile,某个第三方指定源

    项目中有个指定了源,摸索好久Podfile编写方式,网上都没有 pod 'SDK名字', :source => '指定源' 其他的直接按原来的就可以了