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. 【Spring源码深度解析学习系列】Bean的加载(六)

    Bean的加载所涉及到的大致步骤: 1)转换对应beanName 为什么需要转换beanName呢?因为传入的参数可能是别名,也可能是FactoryBean,所以需要一系列的解析,这些解析内容包括如下 ...

  2. 【大数据系列】hadoop集群的配置

    一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml     hdfs-default.xml    mapred-default.xml   mapred-que ...

  3. Window 命令行神器:cmder

    http://cmder.net/ https://github.com/cmderdev/cmder/releases/   官网下载地址 http://www.360doc.com/content ...

  4. Artech的MVC4框架学习——第八章View的呈现

    总结:定义在controller中的action方法一般会返回actionResult的对象对请求给予 响应.viewResult是最常见也是最重要的ActionView的一种(p411).view模 ...

  5. wget 无法建立ssl连接 [ERROR: certificate common name ?..ssl.fastly.net?.doesn?. match requested host name ?.ache.ruby-lang.org?. To connect to cache.ruby-lang.org insecurely, use ?.-no-check-certificate?]

    通过wget下载文件,报错 [root@Redmine-186 opt]# wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.6.tar.g ...

  6. Java内存泄露监控工具:JVM监控工具介绍【转】

    jstack?-- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...

  7. [工具] Snipaste

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

  8. python----题库(一)

    1.执行 Python 脚本的两种方式 答:1.>>python ../pyhton.py 2. >>python.py #必须在首行有 #!/usr/bin/env pyth ...

  9. FZU 2092 收集水晶(记忆化搜索)

    Problem 2092 收集水晶 Accept: 101 Submit: 439 Time Limit: 5000 mSec Memory Limit : 32768 KB Problem Desc ...

  10. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...