28. Triangle && Pascal's Triangle && Pascal's Triangle II

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) {
        vector<int> pSum(triangle.size()+1, 0);
        for(int i = triangle.size()-1; i >= 0; --i)
            for(int j = 0; j < triangle[i].size(); ++j)
                pSum[j] = min(pSum[j]+triangle[i][j], pSum[j+1]+triangle[i][j]);
        return pSum[0];
    }
};

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]
]
思想: 简单的动态规划。
class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int> > vec;
        if(numRows <= 0) return vec;

        vec.push_back(vector<int>(1, 1));
        if(numRows == 1) return vec;

        vec.push_back(vector<int>(2, 1));
        if(numRows == 2) return vec;

        for(int row = 2; row < numRows; ++row) {
            vector<int> vec2(row+1, 1);
            for(int Id = 1; Id < row; ++Id)
                vec2[Id] = vec[row-1][Id-1] + vec[row-1][Id];
            vec.push_back(vec2);
        }
        return vec;
    }
};

Pascal‘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?

思想: 动态规划。注意O(k)空间时,每次计算新的行时,要从右向左加。否则,会发生值的覆盖。

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> vec(rowIndex+1, 1);
        for(int i = 2; i <= rowIndex; ++i)
            for(int j = i-1; j > 0; --j) // key, not overwrite
                vec[j] += vec[j-1];
        return vec;
    }
};

28. Triangle && Pascal's Triangle && Pascal's Triangle II

时间: 2024-08-30 00:50:29

28. Triangle && Pascal's Triangle && Pascal's Triangle II的相关文章

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? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: 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] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

[LeetCode][JavaScript]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 bottom

120. Triangle

Problem Statement:  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 to

Poj 1163 The Triangle 之解题报告

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route t

Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)

Triangle A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. Version 1.6 Copyright 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk 2360 Woolsey #H / Berkeley, California 94705-1927 Bugs/comments to [email protected] Creat

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