LeetCode Minimum Path Sum

原创 2015年11月18日 09:04:43

题目:

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.

题意:

给定一个m行n列的二维数组,这二维数组中的值都是非负,找一条从左上角开始到右下角的所有格子和最小的路径。注意:你每次都只能要么向右走要么向下走一步。

题解:

这道题目是很典型的二维的动态规划的题目,看到此题,就应该想到动态规划。LZ此前在一篇博客中看到类似的题型,不过那个是计算最大值,也差不多,换汤不换药的东西。首先就是要找状态,我们注意到一点,到达一个格子的方式最多两种,从左边来(除了第一列)和从上边来(除了第一行),因此为了求出到达当前格子后最少能收集到的和,我们就要去考察那些能够达到当前这个格子的格子,所以状态转移方程也自然有了:

s[i][j] = a[i][j] + min(s[i-1][j],if i > 0;s[i][j-1],if j > 0) ,但是这里要注意的是针对s[0][0]的那一个格子,因为是从一行和一列开始的,所以当遇到s[0][0]的时候,我是直接将其赋为nums[0][0]的那个值,另外当遇到的是第一行和第一列的时候,因为没有s[i-1][j]和s[i][j-1],所以我在设置这些格子的时候采用Integer.MAX_VALUE,这个最大值来做,这样就可以比较最小的那个了。

public class NumArray 
{
	public static int minPathSum(int[][] nums)
	{
		int n = nums.length;
		int m = nums[0].length;
		//System.out.println(m);
		int[][] S = new int[n][m];
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				int increment = nums[i][j];
				//System.out.print(increment + "  ");
				int left = 0,up = 0;
				if(i == 0 && j == 0)
					S[i][j] =increment;
				else
				{
					if(i > 0)
						up = S[i-1][j];
					else 
						up = Integer.MAX_VALUE;
					if(j > 0)
						left = S[i][j - 1];
					else 
						left = Integer.MAX_VALUE;
					int neighbor = 0;
					if(up <= left)
					   neighbor = up;
					else 
					   neighbor = left;
					S[i][j] = neighbor + increment;
				}
			}
		}
		int result = S[n-1][m-1];
		return result;
	}
	public static void main(String[] args)
	{
		int[][] nums = new int[][]{{1,2},{1,1}};
		System.out.println(minPathSum(nums));
	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode64 Minimum Path Sum

Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to...

leetcode[63] 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刷题(C++)——Minimum Path Sum(Medium)

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

LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths  A robot is located a...

LeetCode: Minimum Path Sum [063]

【题目】 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom righ...

leetcode笔记:Minimum Path Sum

题目的大意是,给定一个m*n的网格,每个格子里有一个非负整数,找到一条从左上角到右下角的路径,使其经过的格子数值之和最小,每一步只能向右或向下走。可使用动态规划来解决该问题。

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 Solutions : 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 ...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)