Skip to content

Latest commit

 

History

History
153 lines (126 loc) · 3.7 KB

README_EN.md

File metadata and controls

153 lines (126 loc) · 3.7 KB

中文文档

Description

Given a triangle array, return the minimum path sum from top to bottom.

For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.

 

Example 1:

Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
   2
  3 4
 6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).

Example 2:

Input: triangle = [[-10]]
Output: -10

 

Constraints:

  • 1 <= triangle.length <= 200
  • triangle[0].length == 1
  • triangle[i].length == triangle[i - 1].length + 1
  • -104 <= triangle[i][j] <= 104

 

Follow up: Could you do this using only O(n) extra space, where n is the total number of rows in the triangle?

Solutions

Dynamic programming.

Python3

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        n = len(triangle)
        for i in range(1, n):
            for j in range(i + 1):
                mi = float('inf')
                if j > 0:
                    mi = min(mi, triangle[i - 1][j - 1])
                if j < i:
                    mi = min(mi, triangle[i - 1][j])
                triangle[i][j] += mi
        return min(triangle[n - 1])

Java

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int n = triangle.size();
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i + 1; ++j) {
                int mi = Integer.MAX_VALUE;
                if (j > 0) {
                    mi = Math.min(mi, triangle.get(i - 1).get(j - 1));
                }
                if (j < i) {
                    mi = Math.min(mi, triangle.get(i - 1).get(j));
                }
                triangle.get(i).set(j, triangle.get(i).get(j) + mi);
            }
        }
        int res = Integer.MAX_VALUE;
        for (int val : triangle.get(n - 1)) {
            res = Math.min(res, val);
        }
        return res;
    }
}

C++

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i + 1; ++j) {
                int mi = INT_MAX;
                if (j > 0) mi = min(mi, triangle[i - 1][j - 1]);
                if (j < i) mi = min(mi, triangle[i - 1][j]);
                triangle[i][j] += mi;
            }
        }
        int res = INT_MAX;
        for (int& val : triangle[n - 1]) {
            res = min(res, val);
        }
        return res;
    }
};

Go

func minimumTotal(triangle [][]int) int {
	n := len(triangle)
	for i := 1; i < n; i++ {
		for j := 0; j < i+1; j++ {
			mi := 2000000
			if j > 0 && mi > triangle[i-1][j-1] {
				mi = triangle[i-1][j-1]
			}
			if j < i && mi > triangle[i-1][j] {
				mi = triangle[i-1][j]
			}
			triangle[i][j] += mi
		}
	}

	res := 2000000
	for j := 0; j < n; j++ {
		if res > triangle[n-1][j] {
			res = triangle[n-1][j]
		}
	}
	return res
}

...