[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.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.

Thought process:
  1. Sub-problem: for each number in the triangle, find its minimum path sum to bottom.
  2. Formula: f[i][j] = min(f[i + 1][j], f[i + 1][j + 1]) + triangle[i][j].
  3. Initialization: for every number in the bottom level, f[i][j] = triangle[i][j].
  4. Answer: f[0][0].
The bottom-up approach seems a little unnatural, because the question asks for the minimum path sum from top to bottom. We could do the top-down approach too. The formula will become f[i][j] = min(f[i - 1][j - 1], f[i - 1][j]) + triangle[i][j]. However, not every number has its corresponding triangle[i - 1][j] and triangle[i][j]. So this approach requires checking if the numbers exist. For the sake of convenience, we will use the bottom-up approach.

Solution 1:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int rows = triangle.size();
        if (rows == 0) {
            return 0;
        }
        
        int[][] f = new int[rows][rows];
        
        for (int i = 0; i < rows; i++) {
            f[rows - 1][i] = triangle.get(rows - 1).get(i);
        }
        
        for (int i = rows - 2; i >= 0; i--) {
            for (int j = 0; j <= i; j++) {
                f[i][j] = Math.min(f[i + 1][j], f[i + 1][j + 1]) + triangle.get(i).get(j);
            }
        }
        
        return f[0][0];
    }
}

Time complexity: 
Say n = number of rows, the overall time complexity is O(n^2).
Space complexity: O(n^2).

The space complexity can be reduced to O(n). After we have computed f[i][j] for level i, the results from level i + 1 will not be used again, so we can simplify the formula to f[j] = min(f[j], f[j + 1]) + triangle[i][j].

Solution 2:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int rows = triangle.size();
        if (rows == 0) {
            return 0;
        }
        
        int[]f = new int[rows];
        
        for (int i = 0; i < rows; i++) {
            f[i] = triangle.get(rows - 1).get(i);
        }
        
        for (int i = rows - 2; i >= 0; i--) {
            for (int j = 0; j <= i; j++) {
                f[j] = Math.min(f[j], f[j + 1]) + triangle.get(i).get(j);
            }
        }
        
        return f[0];
    }
}

Time complexity: O(n^2). Space complexity: O(n).

Comments

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula