当前位置:网站首页>Force buckle - 64 Minimum path sum
Force buckle - 64 Minimum path sum
2022-04-22 21:13:00 【Node_ Su】

dp function Overtime
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 Function pruning Overtime
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 Array , From [0,0] Go to the [i,j] The path and
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://yzsam.com/2022/04/202204221817535502.html
边栏推荐
- Summary driven development record
- TensorFlow基础知识
- MySQL specifies field sorting and user-defined sorting location
- Adobe系列错误代码解决方案汇总
- Reflection and annotation
- 【数据清洗、绘图】Dataframe的简易应用
- Hdlbits (10) learning notes - finite state machine (fsm1 - lemmings4)
- SDF accelerate trace
- cuda10. 2. Install torch 1 nine
- 如何用laragon框架运行php文件
猜你喜欢

Webmaster Tools - Webmaster software website - Webmaster Tools website - Webmaster essential tools free

nodejs笔记3

C语言初阶最后一课:倒置字符串(例如:I like beijing.打印成:beijing.like I)

nodejs笔记5

Win10 installation neo4j

Confidence interval and interval estimation

After five years of graduation, how to increase the monthly salary from 5K to 50W +, what core skills do you need to master?

HDLBits(十 一)学习笔记——有限状态机(FSM onehot - Fsm serialdp)

如何用laragon框架运行php文件

2022-4-22 Leetcode 91.解码方法
随机推荐
2022 G3 boiler water treatment national question bank and online simulation examination
Use of dSPACE simulation platform
(1) UART subsystem learning plan
Hdlbits (10) learning notes - finite state machine (fsm1 - lemmings4)
2022-4-22 Leetcode 91.解码方法
HDLBits(十 一)学习笔记——有限状态机(FSM onehot - Fsm serialdp)
Time report填写规则
HDLBits(十)学习笔记——有限状态机(FSM1 - Lemmings4)
[play Lighthouse] build WooCommerce store, enable Alipay to pay in person.
cuda10. 2. Install torch 1 nine
新闻速递 I MobTech通过中国信通院“安全专项评测”
【数据清洗、绘图】Dataframe的简易应用
QTP11. 5 / UFT including Chinese package
重返天梯-L2-025 分而治之 (25 分)
nodejs笔记3
2022起重机械指挥上岗证题库及在线模拟考试
OpenVX-将Image文件[pgm格式]读写为vx_image对象,以及写操作
统计字符数C
新聞速遞 I MobTech通過中國信通院“安全專項評測”
SEREDS解串模块简介以及硬件实现