设为首页 加入收藏

TOP

[LeetCode]Minimum Path Sum
2015-07-20 17:26:17 来源: 作者: 【 】 浏览:4
Tags: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 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 Solution {
	private int f[][];
	private int g[][];
	public int minPathSum(int[][] grid) {
		int m = grid.length;
		int n = grid[0].length;
    	f = new int[m+1][n+1];
    	g= grid;
    	return dfs(m,n);
    }
    
    private int dfs(int m,int n){
    	if(m<1||n<1) return 2000000000;
    	if(m==1&&n==1) return g[0][0];
    	return Math.min(help(m-1,n),help(m,n-1))+g[m-1][n-1];
    }
    
    private int help(int m,int n){
    	if(f[m][n]>0) return f[m][n];
    	return f[m][n] = dfs(m,n);
    }
}



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇URAL 1707. Hypnotoad's Secr.. 下一篇POJ 2769 Reduced ID Numbers 同..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)