【Triangle 】cpp

题目:

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

                }
            }
            return min_sum;
    }
};

tips:

这种做法时间复杂度O(n),空间复杂度O(1)。

思路很简单,就是遍历每层元素的同时,把这一个元素的值更新为走到该位置的最小路径和。

min_sum这个遍历记录当前最小值,其实只有当遍历到最后一层的时候才有用,偷懒就没有改动了。

但是有个缺点就是把原来的triangle的结构都破坏了,研究一下用O(n)额外空间,但不破坏原有triangle的做法。

时间: 2024-10-04 00:51:14

【Triangle 】cpp的相关文章

【Pascal&#39;s Triangle】cpp

题目: 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

leetcode 【 Triangle 】python 实现

题目: 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 1

【Combinations】cpp

题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 代码: class Solution { public: vector<vector<int>> combine

【Anagrams】 cpp

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 代码: class Solution { public: vector<string> anagrams(vector<string>& strs) { vector<string> ret; map<string,vec

【N-Queens】cpp

题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration

【Permutations】cpp

题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 代码: class Solution { public: vector<vector<int>> permute(vector&

【Subsets】cpp

题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [

【3Sum】cpp

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

【4Sum】cpp

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or