[Leetcode]Minimum Path Sum
来源:互联网 发布:js基础数据类型 编辑:程序博客网 时间:2024/06/08 13:35
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类似,同样是用动态规划~递推式是dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]~代码如下~
class Solution: # @param grid, a list of lists of integers # @return an integer def minPathSum(self, grid): if grid is None or len(grid) == 0 or len(grid[0]) == 0: return 0 lenRow, lenCol = len(grid), len(grid[0]) dp = [[0] * lenCol for i in xrange(lenRow)] dp[0][0] = grid[0][0] for i in xrange(1, lenRow): dp[i][0] = dp[i - 1][0] + grid[i][0] for j in xrange(1, lenCol): dp[0][j] = dp[0][j - 1] + grid[0][j] for i in xrange(1, lenRow): for j in xrange(1, lenCol): dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j] return dp[lenRow - 1][lenCol - 1]还有一种一维的动态规划解法,可以参考http://blog.csdn.net/linhuanmars/article/details/22257673
0 0
- LeetCode: Minimum Path Sum
- LeetCode Minimum Path Sum
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- [LeetCode] Minimum Path Sum
- [Leetcode] Minimum Path Sum
- [LeetCode]Minimum Path Sum
- [leetcode]Minimum Path Sum
- LeetCode-Minimum Path Sum
- [leetcode] Minimum Path Sum
- LeetCode - Minimum Path Sum
- LeetCode:Minimum Path Sum
- leetcode minimum path sum
- 【leetcode】Minimum Path Sum
- 【LeetCode】Minimum Path Sum
- Leetcode: Minimum Path Sum
- Leetcode Minimum Path Sum
- LeetCode Minimum Path Sum
- 读《在前行中蜕变》有感(一)
- Linux学习笔记之基本命令四(输入输出管道邮件)
- SQL中自定义函数的存储过程的区别
- windows 环境下django环境的搭建
- 工作总结
- [Leetcode]Minimum Path Sum
- C语言中 有符号数、无符号数、整数溢出
- 在iOS上绘制自然的签名
- 1月21日总结
- php当中的观察者模式,在laravel框架中有用到
- Yii2 使用三 使用数据库
- IOS资源适配
- 对STM32的NVIC_PriorityGroupConfig使用及优先级分组方式理解
- ZOJ Problem Set - 2857 Image Transformation