Python | Leetcode Python题解之第64题最小路径和

题目:

题解:

python 复制代码
class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        if not grid or not grid[0]:
            return 0
        
        rows, columns = len(grid), len(grid[0])
        dp = [[0] * columns for _ in range(rows)]
        dp[0][0] = grid[0][0]
        for i in range(1, rows):
            dp[i][0] = dp[i - 1][0] + grid[i][0]
        for j in range(1, columns):
            dp[0][j] = dp[0][j - 1] + grid[0][j]
        for i in range(1, rows):
            for j in range(1, columns):
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        
        return dp[rows - 1][columns - 1]
相关推荐
陈天伟教授1 天前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
2301_764441331 天前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
爱笑的眼睛111 天前
GraphQL:从数据查询到应用架构的范式演进
java·人工智能·python·ai
BoBoZz191 天前
ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择
python·vtk·图形渲染·图形处理
liwulin05061 天前
【PYTHON-YOLOV8N】如何自定义数据集
开发语言·python·yolo
木头左1 天前
LSTM量化交易策略中时间序列预测的关键输入参数分析与Python实现
人工智能·python·lstm
电子硬件笔记1 天前
Python语言编程导论第七章 数据结构
开发语言·数据结构·python
HyperAI超神经1 天前
【vLLM 学习】Prithvi Geospatial Mae
人工智能·python·深度学习·学习·大语言模型·gpu·vllm
逻极1 天前
Python MySQL防SQL注入实战:从字符串拼接的坑到参数化查询的救赎
python·mysql·安全·sql注入
赫凯1 天前
【强化学习】第一章 强化学习初探
人工智能·python·强化学习