/**
* 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. 学习笔记整理之StringBuffer与StringBulider的线程安全与线程不安全

    关于线程和线程不安全: 概述 编辑 如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就 ...

  2. web端代码提示

    web端代码提示 这个功能是基本完成了,但是与需求不一致.但是废弃挺可惜的,所以就单独拿出来作为一个例子记录一下. 其中还包括了,java代码的自动编译和执行,在web端显示执行结果. 下载链接: h ...

  3. c刷题

    1.转义字符: C中定义了一些字母前加 "\" 来表示常见的那些不能显示的ASCII字符,如\0 空字符,\r 回车, \n换行等,就称为转义字符,因为后面的字符,都不是它本来的A ...

  4. python2 python3区别

    Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第 ...

  5. robotframework-databaselibrary安装步骤

    我这里主要介绍离线安装的方式 第一步:下载robotframework-databaselibrary-0.6 包可以去网上找安装包下载,如果实在找不到可以联系我 第二步:下载PyMySQL-0.9. ...

  6. spark随笔

    spark基于RDD成功构建起大数据处理的一体化解决方案,将MappReduce.Streaming.SQL.Machine Learning.Graph Processing等 大数据计算模型统一到 ...

  7. Eclipse项目里面看源码和文档

    Eclipse项目里面看源码 1.新建项目列表 2.进入struts2-core-2.3.20.jar,双击之后,看不到源码 3.右键struts2-core-2.3.20.jar,选择propert ...

  8. css格式比较及选择器类型总结

    在前端入门的前三天把网页制作过程中常用的一些标签和属性都认识和练习了一遍,能够做出简单模块的框架.就像老师说的网页制作就像建一栋大楼,html是砖和水泥,css是精装,js是完善各个功能.现在就开始进 ...

  9. Visual Studio 常见的快捷键

    “Ctrl + -”               回到上一个光标位置 “Ctrl + Shift + -”                前进到下一个光标位置 “Ctrl + C”           ...

  10. Redhat/CentOS7-环境虚拟机简单搭建Nginx+Tomcat负载均衡集群

    Tomcat服务器是一个免费的开放源代码的web应用服务器,属于轻量级应用服务器,是开发和调试JSP程序的首选.由于Tomcat处理静态HTML的能力运不及Apache或者Nginx,所以Tomcat ...