Minimum Path Sum leetcode java
题目:
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.
题解:
这道题跟Unique Path系列是思路一样的。具体思路看代码就很清楚了
代码如下:
 1     public int minPathSum(int[][] grid) {
 2         int m = grid.length;
 3         int n = grid[0].length;
 4         
 5         if(m==0||n==0)
 6             return 0;
 7             
 8         int[][] dp = new int[m][n];
 9         
         dp[0][0]=grid[0][0];
         
         //a row
         for (int i = 1; i < n; i++) 
             dp[0][i] = dp[0][i - 1] + grid[0][i];  
 
         //a column
         for (int j = 1; j < m; j++)   
             dp[j][0] = dp[j - 1][0] + grid[j][0];  
         
         for (int i=1; i<m; i++){  
                 for (int j=1; j<n; j++){  
                     if(dp[i-1][j]<dp[i][j-1])
                         dp[i][j]=dp[i-1][j]+grid[i][j];
                     else
                         dp[i][j]=dp[i][j-1]+grid[i][j];
                 }  
             }  
             return dp[m-1][n-1];  
     }												
											Minimum Path Sum leetcode java的更多相关文章
- 【LeetCode】Path Sum  ---------LeetCode  java  小结
		Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ... 
- Binary Tree Maximum Path Sum leetcode java
		题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ... 
- Minimum Path Sum [LeetCode]
		Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ... 
- Minimum Path Sum——LeetCode
		Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ... 
- Path Sum leetcode java
		题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ... 
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
		64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ... 
- 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] 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 ... 
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ... 
随机推荐
- 使用Plant Simulation连接SQL Server
			1. 在管理类库中添加ODBC. 2. 在控制面板->管理工具中设置ODBC,添加SQL Server服务. 3. 在plant simulation中将信息流中的ODBC添加到Frame中. ... 
- hdu 4442 37届金华赛区 A题
			题意:给出一些队伍,每个队伍有初始等待时间和每秒增加的时间,求最短时间 假设有两个队初始时间和每秒增加时间为a1,b1和a2,b2 若第选择第一个的时间小于第二个,则 a1+a2+a1*b2<a ... 
- Django-Filter源码解析一
			Django Filter源码解析 最近在看Django-FIlter项目的源码,学习一下别人的开发思想: 整体介绍 首先,我从其中一个测试用例作为入口,开始了debug之路,一点一点的断点,分析它的 ... 
- VMware安装MikroTik RouterOS chr
			简单步骤: 1.官网下载ova镜像 2.导入到vmware即可. 
- 开源的服务发现项目Zookeeper,Doozer,Etcd - 木精灵的技术博客 - CSDN博客
			开源的服务发现项目Zookeeper,Doozer,Etcd - 木精灵的技术博客 - CSDN博客 http://blog.csdn.net/shlazww/article/details/38 ... 
- 读写文件:每次读入大文件里的一行、读写.CSV文件
			读文件: 传统的读法.所有读出,按行处理: fp=open("./ps.txt", "r"); alllines=fp.readlines(); fp.clos ... 
- WCF消息传递
			通过了解了WCF的一些基本概念并创建和编写WCF应用中的相应方法,实现了WCF服务和客户端之间的调用,就能够理解WCF应用是如何进行通信的.了解了一些基本的WCF概念后,还需要深入了解WCF消息的概念 ... 
- Linux内存管理学习2 —— head.S中的段页表的建立
			作者 彭东林 pengdonglin137@163.com 平台 TQ2440 Qemu+vexpress-ca9 Linux-4.10.17 正文 继续分析head.S: 此时r2存放的是设备树镜像 ... 
- 搭建《深入Linux内核架构》的Linux环境
			作者 彭东林 pengdonglin137@163.com 软件 Host: Ubuntu14.04 64 Qemu 2.8.0 Linux 2.6.24 busybox 1.24.2 gcc 4.4 ... 
- 理解 HTTPS 协议
			英文原文:Understanding HTTPS Protocol 最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如 ... 
