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. SQL Server 优化总结

    1.作为过滤条件字段的数据表,在拼接语句尽量优先拼接,以提升查询效率

  2. 【数据库系列】MySql中的select的锁表范围

    由于InnoDB预设的是Row-Level Lock,只有明确指定主键的时候MySql才会执行Row lock,否则MySql将会执行Table Lock. 1.明确指定主键则是行锁 2.明确指定主键 ...

  3. OpenStack网络详解

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. Openstack需要对网络有一些了解才能进入openstack的世界,很多都是 ...

  4. linux strace 命令详解

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  5. 【转】C内存管理

    在任何程序设计环境及语言中,内存管理都十分重要.在目前的计算机系统或嵌入式系统中,内存资源仍然是有限的.因此在程序设计中,有效地管理内存资源是程序员首先考虑的问题. 第1节主要介绍内存管理基本概念,重 ...

  6. [工具] Snipaste

    https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...

  7. 【Android】Android import和export使用说明 及 export报错:jarlist.cache: Resource is out of sync with the file syst解决

    在Android开发export项目时发现有时会报错,内容如下: Problems were encountered during export:  Error exporting PalmIdent ...

  8. linux消息队列编程实例

    转自:linux 消息队列实例 前言: 消息队列就是一个消息的链表.可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向其中按照一定的规则添加新消息:对消息队列有读权 ...

  9. python基础-动态加载lazy_import(利用__import__)

    看了一天动态加载,普遍有这么几种方法,总结一下,由简入深,本文仅对查到的栗子们做个引用……省去你们大把查资料的时间= = 主要思想:把模块(文件)名.类名.方法名当成了变量 然后利用__import_ ...

  10. Java-字符串加密

    1设计思想: 改程序是对小写的a到z进行加密,输入一段字符串str,输入加密的密匙k,判断录入的的字符与 ‘z’-k+1的大小,比其小的直接加上密匙转化为新的字符,大于的加(k-26)将最后几位字符转 ...