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:

Input:
[
  [1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. 其实这个题目的思路是跟[LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming很像, 只不过一个是步数, 一个是minimal sum而已. 还是可以用滚动数组的方法, 只是需要注意
初始化即可. 1. Constraints
1) [0*0] 最小 2. Ideas Dynamic Programming T: O(m*n) S: O(n) optimal(using rolling array) 3. Code
 class Solution(object):
def minPathSum(self, grid):
# S; O(m*n)
if not grid or len(grid[0]) == 0: return 0
m, n = len(grid), len(grid[0])
if m == 1 or n == 1: return sum(sum(each) for each in grid)
ans = grid
for i in range(m):
for j in range(n):
if i == 0 and j!= 0:
ans[i][j] = ans[i][j-1] + grid[i][j]
if j == 0 and i!= 0:
ans[i][j] = ans[i-1][j] + grid[i][j]
if j >0 and i >0 :
ans[i][j] = min(ans[i-1][j], ans[i][j-1]) + grid[i][j]
return ans[-1][-1]

2)

class Solution:
def minPathSum(self, grid):
m, n = len(grid), len(grid[0])
mem = [[0]* n for _ in range(m)]
mem[0][0] = grid[0][0]
for i in range(1, m):
mem[i][0] = grid[i][0] + mem[i - 1][0]
for i in range(1, n):
mem[0][j] = grid[0][j] + mem[0][j - 1]
for i in range(1, m):
for j in range(1, n):
mem[i][j] = grid[i][j] + min(mem[i - 1][j], mem[i][j - 1])
return mem[m - 1][n - 1]

4. Test cases

[
  [1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7

[LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  3. [LeetCode] 64. Minimum Path Sum 最小路径和

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

  4. LeetCode 64. Minimum Path Sum(最小和的路径)

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

  5. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  7. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  8. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. jstorm开发指南-写个简单的jstorm应用

    jstorm开发指南-写个简单的jstorm应用 发表于 2015-07-18   |   分类于 大数据   |   暂无评论 jstorm 是阿里巴巴开源的基于storm采用Java重写的一套分布 ...

  2. win7 64位系统下读写access数据库以及安装了office32位软件再安装64位odbc的方法

    公司一款软件还在读写access数据库. 问题是我的电脑是win7 64位, 运行程序会报错, 出错信息很明显, 大意是ODBC数据源读写出错. 因此,我需要下载Access ODBC 64位数据源 ...

  3. laravel连sql server报invalid handle returned问题解决方案

    https://github.com/Microsoft/msphpsql/issues/116 4.1.1驱动下载地址 https://github.com/Microsoft/msphpsql/r ...

  4. 第二步 (仅供参考) sencha touch 使用cmd打包apk

    最新版本的cmd可以直接将sencha touch项目打包成本地应用,不过还有很多不足,本文仅供参考 通过sencha app build native命令可以直接将项目打包成本地应用,不过在命令运行 ...

  5. php底层变量分析

    <?php //数组引用 $arr = array('a','b','c','d'); $x = & $arr[1]; $tmp = $arr; $arr[1] = 'e'; echo ...

  6. [SCOI2011]飞镖[数学模拟]

    2335: [SCOI2011]飞镖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 482  Solved: 152[Submit][Status][ ...

  7. OpenCV获取IP摄像头视频

    从开源中国博客搬来,合并博客 实验室做一个智能小车的小项目,期间涉及到在PC端处理小车摄像头的视频.这里先用安卓手机代替一下进行试验.大致流程就是手机摄像头获取视频,开启一个IP摄像头服务软件,在局域 ...

  8. 【咸鱼教程】Egret可长按识别二维码(精确位置和大小)

    教程目录一 实现原理二 实现过程三 Demo下载 本教程是在Egret中实现长按识别的二维码,并可以精确定位二维码的位置和大小,支持横屏和竖屏. 一 实现原理 微信中长按识别二维码,需要长按jpg或p ...

  9. vuejs学习资料

    Vue.js 是一个轻巧.高性能.可组件化的MVVM库,同时拥有非常容易上手的API,让编写动态的UI界面变得轻松简单. 这里是我整理的相关学习资料: vue.js 中文api vue.js gith ...

  10. vim配置函数跳转(c/c++)

    暂时草记一下,有时间好好整理 ctags 如果只是查看函数与变量是在哪里定义的,用ctags就可以了. ctrl+]跳到定义的地方,ctrl+t跳回来. 想要像IDE那样在旁边显示函数与变量列表,用t ...