【LeetCode】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.


public class Solution {
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
if(triangle.size()==0)
return 0;
int len = triangle.size();
int[] re = new int[len];
re[0]=triangle.get(0).get(0);
int min=re[0];
for(int i=1;i<triangle.size();i++){
ArrayList<Integer> al = triangle.get(i);
int pre = 0;

for(int j=0;j<al.size();j++){
if(j==0){
pre=re[0];
re[0]+=al.get(j);
min=re[0];
continue;
}
if(j==al.size()-1){
re[j]=pre+al.get(j);
if(re[j]<min)
min=re[j];
continue;
}
int temp = re[j];
re[j]=Math.min(pre, re[j])+al.get(j);
pre = temp;
if(re[j]<min)
min=re[j];
}

}
return min;

}
}

【LeetCode】Triangle

时间: 2024-12-29 04:02:56

【LeetCode】Triangle的相关文章

【LeetCode】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 

【LeetCode】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 

【LeetCode】triangle求最小和路径

今天闲来无事,做了三道leetcode水题,怒更两发博客,也挺不错.今天更新的两篇都是dp解法的简单题. 题目要求如下. 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],

【leetcode】Triangle (#120)

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 b

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】【Python题解】Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 要求输入一个整数,返回一个表示杨辉三角的数组.我的方法是计算通项公式,首先是编写阶乘函数,然后计算C00,C10,C11即可 利用Python 的map嵌套可以很简洁地实现,核心代码只有一行! class So

【leetcode】118. Pascal&#39;s Triangle

@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) /************************ * @description: simple. * @time_complexity: O(n) * @space_complexity: O(n) ****

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-triangle-ii/ 题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路: 要求空间复杂度为O

【LeetCode】119 - Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){