当前位置:网站首页>Leetcode topic -- 120 Minimum length sum of triangles, simple DP
Leetcode topic -- 120 Minimum length sum of triangles, simple DP
2022-04-21 07:52:00 【emo~~~~】
Topic link
Don't think about doing this with greed ! Greedy code is not only long , And it's easy to make mistakes . Besides, this is a dp The template questions , You don't even have to push the equation , I'm sure I don't even want to directly dp 了
Pay attention to some points. :
1、dp From the top down , But the title says from bottom to top , When writing recursion, don't get it upside down
2、 Pay attention to the limited array size and data range .
in addition , The main function input of this problem is also very interesting , I wrote it for you , I hope to share with you
// The minimum path of a triangle and
int minimumTotal(vector<vector<int>>& triangle) {
int len = triangle.size();
if (len == 1)
{
return triangle[0][0];
}
int dp[100][100] = { 0 };
int ans = 0;
int i = 0;
dp[0][0] = triangle[0][0];
for (int i = 1; i < len; i++)
{
for (int j = 0; j < triangle[i].size(); j++)
{
if (j - 1 >= 0 && j < triangle[i - 1].size())
{
dp[i][j] = triangle[i][j] + min(dp[i - 1][j], dp[i - 1][j - 1]);
}
else if(j - 1 < 0)
{
dp[i][j] = triangle[i][j] + dp[i - 1][j];
}
else
{
dp[i][j] = triangle[i][j] + dp[i - 1][j - 1];
}
}
}
int minn = 100000;
for (int i = 0; i < len; i++)
{
minn = min(minn, dp[len - 1][i]);
}
return minn;
}
int main()
{
vector<vector<int>>tr;
int n = 1;
for (int i = 0; i < 3; i++)
{
vector<int>ret;
for (int j = 0; j < n; j++)
{
int a = 0;
cin >> a;
ret.push_back(a);
}
tr.push_back(ret);
ret.clear();
n++;
}
int ans = minimumTotal(tr);
cout << ans;
return 0;
}
版权声明
本文为[emo~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210637082760.html
边栏推荐
- 批量压缩文件夹里的sql备份bak为rar,然后删除3天之前的rar
- 2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中
- Flutter初体验
- php 中文转化为英文首字母
- C语言指针进阶(1.一阶与二阶指针)
- Panic and Recover
- Record the problems and solutions encountered in using fastjson message converter
- Flutter first experience
- Fuzzy query between two tables of SQL server and steps of importing Excel data into SQL Server
- 网关与分布式id
猜你喜欢

Use case diagram of umlet instructions

IIC总线设计①——IIC通信协议

服务器部署svn环境

343. 求分解整数的乘积最大化Integer Break

服务器部署svn环境

Fuzzy query between two tables of SQL server and steps of importing Excel data into SQL Server
![Navicat even Oracle reports an error [no matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat even Oracle reports an error [no matching authentication protocol]

从零开始自制实现WebServer(十五)---- 日志库部分完结啦 实用小件DOUBLE-BUFFERING优化异步写入性能

Shortcut keys for batch modification of variable name and batch replacement code in idea

PLSQL developer 14 installation details
随机推荐
php 输出两个指定日期中间的所有时间
微信公众号查看uin,把它base64后即为biz
GoLang学习资源清单
leetcode 27. Removing Elements
2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中
playwright click timeout属性,等待元素出现的时间
laravel execl导入时间变数字
@Slf4j注解中 log 报错
Leetcode 707 Design linked list
Plsql14 software package download, localization and registration
记录使用fastjson消息转换器遇到的问题和解决方法
Postgre (PG) - SQL script record
asp. Net re regular expression, filter the amount of time order number in the returned string, and convert the non-standard time format into the correct time format
Axure产品原型工具使用笔记
MySQL5.7安装操作手册(Centos7)
实体类映射一个字段
Golang learning resource list
Enterprise service bus -- Introduction to muleesb
You cannot set a form field before rendering a field associated with the value
C#linq的group、count、sum,记一下
https://leetcode-cn.com/problems/triangle/