leetcode — minimum-path-sum
/**
* 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的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- 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 ...
- [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 ...
- 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 ...
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- LeetCode:Minimum Path Sum(网格最大路径和)
题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- LeetCode Minimum Path Sum (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- [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 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- 关于Bell数的一道题目
考虑 T3+1 {1,2,3,4} T3是3个元素的划分,如果在里面加入子集{4}, 4被标成特殊元素, 就形成了T4一类的划分(里面的子集的并集是{1,2,3,4}) T2是2个元素的划 ...
- pdf流文件的展示、下载、打印;html转为pdf
背景:合同(后台返回pdf流文件)展示.下载.打印,基于angular4 场景区分: 1.checkout页面 —— post接口,入参为offering.shippingInfo.invoice等( ...
- 逻辑回归 vs 决策树 vs 支持向量机(II)
原文地址: Logistic Regression vs Decision Trees vs SVM: Part II 在这篇文章,我们将讨论如何在逻辑回归.决策树和SVM之间做出最佳选择.其实 第一 ...
- linux pxe 安装Centos7
服务端 需要3种服务 dhcp + tftp + vsftp tftp 提供引导 为什么不使用其他协议来进行pxe引导 是因为网卡只会集成tftp这种服务 写明到镜像的方式 dhcp 下发tf ...
- rails5 后台入门(api mode)
1. Installation (Centos为例) 1.1安装rvm (http://rvm.io/) gpg2 --recv-keys 409B6B1796C275462A1703113804BB ...
- 如何高效地写CSS--等以后有空多加总结一下
CSS写的并不多,如果从零开始的项目,自己一定想搬砖来得容易点.CSS编写一定有其工程化的方法,来时编写更加有效率. 考虑将CSS的预处理LESS.Sass或Stylus引入,或者将CSS的后处理Po ...
- 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务
[源码下载] 背水一战 Windows 10 (115) - 后台任务: 通过 toast 激活后台任务, 定时激活后台任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 通 ...
- Docker应用:Docker-compose(容器编排)
阅读目录: Docker应用:Hello World Docker应用:Docker-compose(容器编排) 前言: 昨天完成了Docker入门示例(Docker应用:Hello World),示 ...
- 前端开发掌握nginx常用功能之rewrite
上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足 ...
- PackageManager整理
一.PackageManager的功能 1.安装.卸载应用.2.查询permission相关信息.3.查询Application相关信息(application,activity,receiver,s ...