120. Triangle

Key idea: memorize search+from bottom to top

List<List<Integer>> triangle actual shape

(0,0) -> (1,0), (1,1)

(1,0) -> (2,0), (2,1) ; (1,1) -> (2,1), (2,2) ;

(2,0) -> (3,0), (3,1) ; (2,1) -> (3,1), (3,2) ; (2,2) -> (3,2), (3,3) ;

........

According to above, (2,1) (3,1) (3,2) are repeated. If do regular DFS, every node has 2 choices, left or right in the next row. Therefore, time complexity is O(2n)O(2^n) . So another method is to store all the minimal path from any node to the last row. We let dfs(int x, int y) records the minimal path sum from (x,y) to the last row.

sumPath[x][y] = min(dfs(x+1, y),dfs(x+1, y+1)) + traingle[x][y]

Space complexity is O(n2)O(n^2) (sumPath[][]). Because every node is only needed to compute once, there are (n(n+1))/2(n(n+1))/2 times of computation, if every computation is O(1)O(1) , then time complexity is O(n2)O(n^2)

If we do from bottom to up to record the minimal path for each node, we only need a int[] minPath to store the minimal path for each node of next row. Then taking advantage of minPath[] infers the minimal path of each node in current row. The result is minPath[0][0]. The space complexity is O(n)O(n) , the time complexity is O(n2)O(n^2)

Advantage: easy to think and implement

Disadvantage: Expensive memory cost

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        miniPath = [[0] * len(triangle[-1])] * len(triangle)
        for i in range(len(triangle[-1])):
            miniPath[-1][i] = triangle[-1][i]
            
        for i in reversed(range(len(triangle)-1)):
            for j in range(len(triangle[i])):
                miniPath[i][j] = min(miniPath[i+1][j], miniPath[i+1][j+1]) + triangle[i][j]
        
        print(miniPath)
        return miniPath[0][0]

Python example 2: space complex = O(n)O(n)

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        for(int i = triangle.size() - 2; i >= 0; i --)
            for(int j = triangle.get(i).size() - 1; j >= 0; j --) {
                int value = triangle.get(i).get(j) + Math.min(triangle.get(i+1).get(j), triangle.get(i+1).get(j+1));
                triangle.get(i).set(j, value);
            }
        return triangle.get(0).get(0);
    }
}

Last updated

Was this helpful?