LeetCode||64. Minimum Path Sum
来源:互联网 发布:java二分递归 编辑:程序博客网 时间:2024/06/15 04:01
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.
Example 1:
[[1,3,1], [1,5,1], [4,2,1]]Given the above grid map, return
7
. Because the path 1→3→1→1→1 minimizes the sum.这题还是类似的动态规划,判断条件变为取两个数之间最小的。
因此,例子中的结果矩阵为:
[[1,4,5], [2,7,6], [6,8,7]]所以结果为7
class Solution(object): def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ row = len(grid) col = len(grid[0]) res = [[0 for i in range(col)] for j in range(row)] res[0][0] = grid[0][0] for i in range(1, row): res[i][0] = res[i-1][0] + grid[i][0] for i in range(1, col): res[0][i] = res[0][i-1] + grid[0][i] for i in range(1, row): for j in range(1, col): res[i][j] = min(res[i-1][j], res[i][j-1]) + grid[i][j] return res[-1][-1]
阅读全文
0 0
- [LeetCode]64.Minimum Path Sum
- LeetCode --- 64. Minimum Path Sum
- LeetCode 64.Minimum Path Sum
- [Leetcode] 64. Minimum Path Sum
- [leetcode] 64.Minimum Path Sum
- [leetcode] 64.Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- 64. Minimum Path Sum LeetCode
- [LeetCode]64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- [leetcode] 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode *** 64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- 【leetcode】64. Minimum Path Sum
- Leetcode:64. Minimum Path Sum
- LeetCode-64.Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- 【Scikit-Learn 中文文档】使用 scikit-learn 介绍机器学习 | ApacheCN
- RANSAC LMedS 详细分析
- 阿里云营销引擎使用教程
- json数据的转换
- Unity3D和Android相互通信的坑1
- LeetCode||64. Minimum Path Sum
- 【Scikit-Learn 中文文档】广义线性模型
- Android 开发笔记 1:Ubuntu 14.04 中安装eclipse开发环境
- HDU 1305:Immediate Decodability
- 使用Maven私服的好处
- 如何在交互式shell脚本中创建对话框
- Unreal教学(4)——组件和碰撞(Components and Collision)
- 禅道布署
- dbutil 的使用方法