当前位置:网站首页>golang刷leetcode动态规划(12)最小路径和
golang刷leetcode动态规划(12)最小路径和
2022-08-02 18:54:00 【用户9710217】
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
示例:
输入:
[
[1,3,1],
[1,5,1],
[4,2,1]
]输出: 7
解释: 因为路径 1→3→1→1→1 的总和最小。
解题思路
1,这也是一个典型的动态规划题
2,是递增的
3,状态转移方程为
if step[i-1][j]<step[i][j-1]{
step[i][j]=step[i-1][j]+grid[i][j]
}else{
step[i][j]=step[i][j-1]+grid[i][j]
}归纳总结
1,这种矩阵寻找路径类型的题目基本都是动态规划题目
2,动态规划问题都可以递归解,只不过利用空间换时间,存储了最优子结构
3,动态规划主要考察的是问题拆分能力,将一个问题拆分为一个个小问题,然后各个击破。
代码实现
func minPathSum(grid [][]int) int {
if len(grid)==0{
return 0
}
step:=make([][]int,len(grid))
for i:=0;i<len(grid);i++{
step[i]=make([]int,len(grid[0]))
}
step[0][0]=grid[0][0]
for i:=1;i<len(grid);i++{
step[i][0]=step[i-1][0]+grid[i][0]
}
for i:=1;i<len(grid[0]);i++{
step[0][i]=step[0][i-1]+grid[0][i]
}
for i:=1;i<len(grid);i++{
for j:=1;j<len(grid[0]);j++{
if step[i-1][j]<step[i][j-1]{
step[i][j]=step[i-1][j]+grid[i][j]
}else{
step[i][j]=step[i][j-1]+grid[i][j]
}
}
}
return step[len(grid)-1][len(grid[0])-1]
}边栏推荐
- C#里如何简单的校验时间格式
- 快手web did可用生成
- 脑机接口003 | 马斯克称已实现与云端的虚拟自己对话,相关概念股份大涨
- Therapy | How to Identify and Deal with Negative Thoughts
- T5: Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer
- Gradle系列——Gradle的build.gradle文件详情,项目发布(基于Gradle文档7.5)day3-3
- 7.24 - 每日一题 - 408
- Monitor is easy to Mars debut: distributed operations help TOP3000 across management gap
- 流量分析三—远程登陆
- 安装Mac版Mysql卡在Installation阶段,彻底清理mysql并重装mysql
猜你喜欢

【C语言刷题】双指针原地修改数组(力扣原题分析)
![[Dynamic Programming Special Training] Basics](/img/62/a647783484d0e600f91924a345af07.png)
[Dynamic Programming Special Training] Basics

你想要的宏基因组-微生物组知识全在这(2022.8)

Unity 打包和切换平台|Build Settings窗口介绍

微服务-gateway【服务网关入门】

阿里测试8年经验,靠着这份理解,我才得以生存下来

Functional test points for time, here is a comprehensive summary for you

NC | 土壤微生物组的结构和功能揭示全球湿地N2O释放

T31开发笔记:metaipc测试

Golang swagger :missing required param comment parameters
随机推荐
想通过FC连接RDS mysql。是不是将FC服务角色添加rds权限后,就可以通过地址,端口建连了呢
中国科学院院属研究单位
请教下,1.0.0和1.0.2的底层数据库表结构有变化吗?
微服务-gateway【服务网关入门】
Detailed explanation of AtomicInteger
Go----Go 语言快速体验之开发环境搭建及第一个项目HelloWord
Monitor is easy to Mars debut: distributed operations help TOP3000 across management gap
7.23 - 每日一题 - 408
【动态规划专项训练】基础篇
有哪些好用的实时网络流量监控软件
E. Add Modulo 10(规律)
What are the useful real-time network traffic monitoring software
洛谷P1502 窗口的星星
动态生成不同类型的订单,请问如何存放到Mongodb数据库?
回收站删除的文件怎么恢复,2个方法汇总助您快速解决
Based on OpenGL glaciers and firebird (illumination calculation model, visual, particle system)
openlayers version update difference
情景剧《重走长征路》上演
EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
From the technical panorama to the actual scene, analyze the evolutionary breakthrough of "narrowband high-definition"