【一天一道LeetCode】#120. Triangle
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
(二)解题
题目大意:给定一个三角矩阵,求从上到下的路径中和最小的路径。每次向下走只能从相邻的数走。
解题思路:Note中提到空间复杂度要为O[n],n为最下面一行数的个数
这种问题一般都会想到动态规划,所以直接往转移方程上想。
记dp[i][j]为(i,j)点到最低端的最小路径和。n为矩阵的深度
则从第n-1行开始,dp[i][j] += min(dp[i+1][j],dp[i+1][j+1]),一路往上计算,最终dp[0][0]即为所求。
于是很快写出代码,10msAC版本。
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.empty()) return 0;
vector<vector<int>> dp = triangle;
int n = dp.size();
if(n==1) return triangle[0][0];//只有一行的时候直接返回
for(int i = n-2 ; i >=0 ; i--)
{
for(int j = 0 ; j < dp[i].size();j++)
{
dp[i][j] += min(dp[i+1][j],dp[i+1][j+1]);//状态转移方程
}
}
return dp[0][0];
}
};
考虑到优化上述代码,使得空间复杂度更低,满足O(n),其实,只用一个一维数组就能解决问题。
下面的代码时优化后的版本,8msAC.
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.empty()) return 0;
int n = triangle.size();
vector<int> dp = triangle[n-1];//空间复杂度更低
for(int i = n-2 ; i >=0 ; i--)
{
for(int j = 0 ; j < triangle[i].size();j++)
{
dp[j] = triangle[i][j]+min(dp[j],dp[j+1]);
}
}
return dp[0];
}
};
【一天一道LeetCode】#120. Triangle的更多相关文章
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode 120]triangle 空间O(n)算法
1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- Java for LeetCode 120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode] 120. Triangle (Medium)
原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径. class Solution { public: int minimumTotal(vector<vector&l ...
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- [leetcode]120.Triangle三角矩阵从顶到底的最小路径和
Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...
随机推荐
- .net 导入excel数据
using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System. ...
- [ 学习笔记 ] Hibernate框架学习之一
一.JavaEE开发三层结构和三大框架的对应关系: Struts2框架 -> 表现层 web层(MVC是表现层的设计模型) 业务层 service层 Hibernate框架 -> 持久层 ...
- pm2快速使用
介绍 pm2 是一个带有负载均衡功能的Node应用的进程管理器..它使您可以永久保持应用程序的活动状态,无需停机即可重新加载应用程序,并且可以方便常见的系统管理任务 特性 行为配置 源地图支持 容器集 ...
- 2. struct A 和 typedef struct A
2. struct A 和 typedef struct A 2.1 struct A struct A{}定义一个名为struct A的结构体. 下例定义了struct A同时,声明了两个变量(注意 ...
- url重定向或者重写
有四种方式:1.urlMappings,返回200状态码 <system.web> <urlMappings > <add url="~/others.aspx ...
- MySQL 数学函数
MySQL 数学函数 所有的数学函数在发生错误的情况下,均返回 NULL. -元减.改变参数的符号 mysql> SELECT - 2; -> -2 注意,如果这个操作符被用于一个 BIG ...
- PHP Zip File 函数
通过 PHP 中的相关函数,你可以实现 zip 文件的解压缩操作! PHP Zip File 简介 Zip File 函数允许您读取压缩文件. 安装 如需在服务器上运行 Zip File 函数,必须安 ...
- 2.docker常用命令
一.安装相关 #查看docker是否安装 rpm -q docker #CentOS下安装docker sudo yum install docker #启动 Docker systemctl s ...
- ThreadLocal(线程绑定)
为保证在DAO层里的操作都在同一事务里,我们曾使用以参数的形式将Connection向下传递的方式,而ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection ...
- 给大家安利一个学习angular2的视频网站
本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 视频地址: https://egghead.io/courses/angular-2-fundamen ...