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 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.

【题意】

给定一个三角,要求从第一层到最后一层的最小路径和,每一步只能移动到下一层的相邻位置,如果当前在A[r][i],表示在第r层的第i个位置,则下一步可以移动到A[r+1][i]或者A[r+1][i+1]位置。

题目要求只能申请O(n)的额外空间,其中n为层数

【思路】

DP问题。申请2个n长度的数组,数组记录了从第一行到达某一行k(k<=n)上每个位置的最短路径和,2个数组则是用来交替存储相邻行的结果。

【代码】

class Solution {
public:
    int minimumTotal(vector<vector<int> > &triangle) {
        int size=triangle.size();
        if(size==0)return 0;

        vector<int> row1(size, 0);  //存储奇数行结果
        vector<int> row2(size, 0);  //存储偶数行结果
        int rowNo=1;    //记录当前正在计算的行号,初始化为1表示我们正在计算从第一层到第一层的最短路径和
        //初始化row1
        row1[0]=triangle[0][0];
        //依次计算到达各层各位置的最短路径和
        while(rowNo<size){
            rowNo++;
            if(rowNo%2==0){
                //从row1得到rowNo行上各位置的最短路径和,并保存到row2中
                for(int i=0; i<rowNo; i++){
                    if(i==0) row2[i]=row1[i]+triangle[rowNo-1][i];
                    else if(i==rowNo-1) row2[i]=row1[i-1]+triangle[rowNo-1][i];
                    else row2[i]=min(row1[i]+triangle[rowNo-1][i], row1[i-1]+triangle[rowNo-1][i]);
                }
            }
            else{
                //从row2得到rowNo行上各位置的最短路径和,并保存到row1中
                for(int i=0; i<rowNo; i++){
                    if(i==0) row1[i]=row2[i]+triangle[rowNo-1][i];
                    else if(i==rowNo-1) row1[i]=row2[i-1]+triangle[rowNo-1][i];
                    else row1[i]=min(row2[i]+triangle[rowNo-1][i], row2[i-1]+triangle[rowNo-1][i]);
                }
            }
        }

        //size是偶数,则从row2获取全局的最短路径和,反之,从row1中获取全局的最短路径和
        int minPathSum=INT_MAX;
        if(size%2==0){
            for(int i=0; i<size; i++)
                if(row2[i]<minPathSum)minPathSum=row2[i];
        }
        else{
            for(int i=0; i<size; i++)
                if(row1[i]<minPathSum)minPathSum=row1[i];
        }
        return minPathSum;
    }
};

LeetCode: Triangle [120]

时间: 2024-10-08 09:48:41

LeetCode: Triangle [120]的相关文章

LeetCode -- Triangle 路径求最小和( 动态规划问题)

人们常说"细节决定成败". 编码工作中,同样需要关注细节. 本文将给出3个小实例来说明编码中关注细节的重要性,同时给出作者对如何注意编码细节的一点见解(说的不对,请指正). 例1 这个问题如此地显而易见,竟然没有被发现. List<int> numList = new List<int>(); numList.Add(3); numList.Add(1); numList.Add(4); numList.Add(2); numList.Add(5); numLi

[LeetCode] Triangle(&#39;Bottom-up&#39; 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], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

Leetcode:Triangle 三角形塔最小路径和

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 botto

【Leetcode】120.Triangle

[Question]   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 bo

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

【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 OJ 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

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

[leetcode]Triangle @ Python

原题地址:https://oj.leetcode.com/problems/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