[LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 = ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- HDFS的客户端操作
命令行操作: -help 功能:输出这个命令参数手册 -ls 功能:显示目录信息 示例: hadoop fs -ls hdfs://hadoop-serv ...
- 转载->C#事件的使用和讲解
C#事件的使用和讲解 事件的由来 在上一篇幅博客中http://www.cnblogs.com/JiYF/p/6867081.html 对委托讲解的比较细致 我们继续思考上面的程序:上面的三个方法都定 ...
- jQuery属性操作(四)
通过阅读jQuery为属性操作封装的基本方法和为处理兼容性问题提供的hooks,发现jQuery在属性操作方面并没有做过多的设计,只是处理一下兼容性问题,然后调用基础的DOM操作方法.以下是对JQue ...
- smarty assign变量赋值
1.变量赋值的两种写法 <%assign var="name" value="cl"%> <%assign "name" ...
- android开发-c++代码调用so库
Android项目的CMakeLists.txt代码如下,so文件放在项目的$Project/app/src/main/jniLibs/$arch下,$arch替换为arm64-v8a armv7a等 ...
- WIN7开启wifi热点
1.首先,先确定自己的笔记本网卡支持“启动承载网络”的功能,使用管理员运行cmd命令.启用管理员运行CMD的方法Windows-所有程序-附件-运行(右键,以管理员身份运行)在弹出的CMD窗口里面敲击 ...
- 用ELK打造可视化集中式日志
原文链接:https://yq.aliyun.com/articles/57420 摘要: Elk是Elastic search, Logstash和Kibana三者的简称. Elastic sear ...
- python unittest框架中doCleanups妙用
偶看unittest官方文档中,发现一个很好用的功能函数doCleanups,看看官方是怎么解释的: doCleanups() This method is called unconditionall ...
- 【问题收录】Ubuntu14.04连接两个双显示器失败的解决方案
https://blog.csdn.net/chichoxian/article/details/60642533
- codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man with a pair o ...