当前位置:网站首页>力扣-64.最小路径和
力扣-64.最小路径和
2022-04-22 18:18:00 【Node_Su】

dp函数 超时
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
n = len(grid[0])
return self.dp(grid, m - 1, n - 1)
def dp(self, grid, i, j):
if i == 0 and j == 0:
return grid[0][0]
if i < 0 or j < 0:
return 99999
res = min(self.dp(grid, i - 1, j), self.dp(grid, i, j - 1)) + grid[i][j]
return res
dp函数剪枝 超时
class Solution(object):
import sys
sys.setrecursionlimit(1000000)
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
n = len(grid[0])
return self.dp(grid, m - 1, n - 1)
def dp(self, grid, i, j):
m = len(grid)
n = len(grid[0])
res = 999999
memo = [[-1 for i in range(m)] for j in range(n)]
if i == 0 and j == 0:
return grid[0][0]
if i < 0 or j < 0:
return res
if memo[i][j] != -1:
return memo[i][j]
memo[i][j] = min(self.dp(grid, i - 1, j), self.dp(grid, i, j - 1)) + grid[i][j]
return memo[i][j]
dp数组,表示为从[0,0]走到[i,j]的路径和
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
n = len(grid[0])
dp = [[0 for i in range(n)] for j in range(m)]
dp[0][0] = grid[0][0]
for i in range(1, m):
dp[i][0] = dp[i - 1][0] + grid[i][0]
for j in range(1, n):
dp[0][j] = dp[0][j - 1] + grid[0][j]
for i in range(1, m):
for j in range(1, n):
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
return dp[m - 1][n - 1]
if __name__ == '__main__':
grid = [[1, 2, 3], [4, 5, 6]]
Sol = Solution()
res = Solution.minPathSum(Sol, grid)
print(res)
版权声明
本文为[Node_Su]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Node_Su/article/details/124343674
边栏推荐
- Usage of SAP ABAP for all entries
- Use of ES6 generator function
- 我为什么不再使用 flomo 了
- 图像的卷积——【torch学习笔记】
- sh文件内容启动文件
- filter执行原理深度剖析(bitset机制与caching机制)
- Design the test paper storage scheme of ten million students management system
- Codeforces round 784 (Div. 4) AK solution
- [fundamentals of interface testing] Chapter 9 | detailed explanation of postman global variables and environment variables
- es6 Generator函数的使用
猜你喜欢

One click download scheme of serial port of esp32 / esp8266 series single chip microcomputer without peripheral circuit

Design the test paper storage scheme of ten million students management system
![[fundamentals of interface testing] Chapter 10 | explain the pre script of postman request and its working principle in detail](/img/b1/64d4996fb27939dfbd98a6f0da8671.png)
[fundamentals of interface testing] Chapter 10 | explain the pre script of postman request and its working principle in detail

Huawei router realizes the connection between headquarters and branches through MPLS virtual private network

Packet capture analysis of interface protocol TCP protocol

【bat】查看文件md5

【Lane】Ultra-Fast-Lane-Detection(2)自定义模型测试

Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241

图像的卷积——【torch学习笔记】

DLL的封装及调用
随机推荐
Recent learning experience
C# 从list 或者string中随机获取一个
常见报错记录
An idea plug-in that doesn't work, but can install X
Youqilin 22.04 lts version is officially released | ukui 3.1 opens a new experience!
Dpdk captures traffic from a given port / queue
即使 Outlook Deleted Items 文件夹清空之后,仍然可以恢复被删除的邮件
JVM composition
es6 Generator函数的使用
电脑硬件中最重要的部分是什么?
Research Report on the development of asset management and custody banking industry in 2021
Flink之转换算子 (Transformation)
SAP UI5 数据类型(data type) 学习笔记
使用docker创建mysql主从备份时遇到的一些问题
小程序----API
Use of ES6 generator function
【Lane】Ultra-Fast-Lane-Detection(1)自定义数据集训练
Spacy first routine (automatic annotation of Chinese text)
C语言读写txt文件
[fundamentals of interface testing] Chapter 11 | detailed explanation of postman associated interface and batch execution use case set