【一天一道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 ...
随机推荐
- 解决ansible首次连接host服务器需验证问题
问题描述: [root@iZm5e79rtwsq2hm57teyk5Z ansible]# ansible aofeng -f 5 -m ping 47.93.18.191 | FAILED! =&g ...
- React 关于组件(界面)更新
在最近在学 React , 将组件的UI更新稍微整理了一下.根据业务要求,可能会出现如下的技术实现要求:1.更新自己2.更新子组件3.更新兄弟组件4.更新父组件5.父 call 子 function ...
- Oracle10g以上sysaux表空间的维护和清理
SYSAUX表空间在Oracle 10g中引入,其作为SYSTEM表空间的辅助表空间.之前,一些使用独立表空间或系统表空间的数据库组件,现在SYSAUX表空间中存在.通过分离这些组件,减轻了SYSTE ...
- 使用pscp实现Windows 和 Linux 服务器间的远程拷贝文件
在工作中,每次部署应用时都需要从本机Windows 服务器拷贝文件到Linux 上,有时还将Linux 上的文件拷到本机,这些操作都是可以使用pscp实现的.下文将详细描述如何使用: PSCP (Pu ...
- PWA初体验
一.前言 现在市面上的Native APP成千上万个,各种应用商店里面的APP琳琅满目.原生的APP下载到手机上之后,用户就可以获取一个方便的入口,体验上也十分顺畅.但是再好的事物难免有点缺点: 1 ...
- Lucene初体验——Hello Word实现
1.创建索引 /** * 建立索引 */ public void index(){ IndexWriter writer=null; try { //1.创建Directory //Directory ...
- FJUT第三周寒假作业《第九集,离间计》栈
第九集,离间计 TimeLimit:1000MS MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 拥有了 ...
- SSA-一种适合中小型企业的新型服务架构
写在前面 好久好久没写了,最近刚换了工作,花了几天的时候熟悉了项目,接着就是功能的完善,随后就是对新项目的基础架构搭建. 看过Po主博客的都知道,Po主一直致力于推广.Net Core在微服务架构上的 ...
- 第一次作业:基于Linux操作系统深入源码进程模型分析
1.Linux操作系统的简易介绍 Linux系统一般有4个主要部分:内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使 ...
- Docker容器的运用
Docker 利用容器来运行应用. 容器是从镜像创建的运行实例.它可以被启动.开始.停止.删除.每个容器都是相互隔离的.保证安全的平台. 可以把容器看做是一个简易版的 Linux 环境(包括root用 ...