[leetcode] 120 Triangle

感觉acm做过之后,这种题太基本了....

没啥好说的,最简单的动态规划,找出状态转移方程就可以了。采用由下到上的思想(这样最后只需要取出dp[0][0]就是答案),本层每个结点的结果根据下面一行的路基累计和而计算,要么取左边的,要么取右边的,两者取最小的即可。

状态转移方程:triangle[i][j] += min(triangle[i + 1][j], triangle[i +
1][j + 1])

class Solution {
public:
  int minimumTotal(vector<vector<int> > &triangle) {
    for (int i = triangle.size() - 2; i >= 0; --i)
      for (int j = 0; j <= i; ++j){

        triangle[i][j] +=min(triangle[i+1][j+1],triangle[i+1][j]);
      }

    return triangle[0][0];
  }
};

类似的题目还有
hdu 2084的数塔,和本题的解法一致。

稍微进阶一点的: hdu 1176 免费馅饼

思路:可将所有的时间段和馅饼看成是一个矩阵,时间就是行数,掉馅饼的就是列数,则就是数字三角形问题,从最底层找一条路径,使得路径上的和最大。状态转移方程为:dp[i][j]=max(dp[i+1][j-1],dp[i+1][j],dp[i+1][j-1])+pie[i][j]。pie[i][j]为时间i时在j位置掉的馅饼数目。

具体代码这里就不贴出了,大家尽量去实现一下。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-29 17:10:47

[leetcode] 120 Triangle的相关文章

[leetcode 120]triangle 空间O(n)算法

1 题目 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 120. Triangle 20170706 部分之前做了没写的题目

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 120 Triangle (Dynamic Programming Method)

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 120 Triangle ----- java

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

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 120道题目

Leetcode 120道题目 [01]191. 位1的个数.231. 2的幂.342. 4的幂 原文地址:https://www.cnblogs.com/sunbines/p/10824622.html

【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