# 关关的刷题日记96 – Leetcode 120. Triangle

## 题目

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.

## 思路

```class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int re=INT_MAX;
for(int i=1; i<triangle.size(); ++i)
{
for(int j=0; j<triangle[i].size(); ++j)
{
if(j==0)
triangle[i][j]=triangle[i-1][j]+triangle[i][j];
else if(j==i)
triangle[i][j]=triangle[i-1][j-1]+triangle[i][j];
else
triangle[i][j]=min(triangle[i-1][j-1],triangle[i-1][j])+triangle[i][j];
}
}
for(int i=0; i<triangle[triangle.size()-1].size(); ++i)
re=min(re,triangle[triangle.size()-1][i]);
return re<INT_MAX?re:0;
}
};```

0 条评论

## 相关文章

### 算法系列

算法对程序员来说是熟悉的陌生人，编过大量代码后突然被哪个问到算法是什么也有时不知从何说起，简单来说是没有好好总结过仔细分析过。大学里面导师整天苦口婆心的教导...

237100

36760

37080

36610

30150

30140

30320

### 1095 火星人

1095 火星人 2004年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 D...

366100

35460

28430