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]
相关推荐
zhanghongyi_cpp20 分钟前
第5章 学习自测
python·学习
打工仔折腾 AI1 小时前
L3 静态源码取证|工程评测:python-oracledb 驱动评估中的样本偏差陷阱
开发语言·人工智能·后端·python·性能优化
IvanCodes2 小时前
LangChain(15):LangSmith 应用评估与回归测试
python·langchain·agent
炸薯条!3 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
jason_renyu4 小时前
Playwright 入门与实操:从安装、定位、断言到自动发博客,把浏览器自动化讲明白
运维·python·自动化·前端开发·浏览器自动化·playwright·e2e测试
维克兜率天4 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
向科→向总工→空间动态计系统5 小时前
机器人大脑系统
开发语言·人工智能·python·机器人
染指11105 小时前
194.DeepAgents-在DeepAgents中使用自定义沙盒环境OpenSandBox
网络·人工智能·python·langchain·deepagents
吞下星星的少年·-·6 小时前
LeetCode 热题 100 最长连续序列(哈希表)
算法·leetcode·散列表
m0_646429976 小时前
LangGraph 学习笔记(一):StateGraph 与状态管理
python·langgraph