My simple Java AC solution with O(n^2) time, O(n) space


  • 0
    C

    I sum the number from triangle bottom, so there is no need to check the minimum sum in array. sum[0] is the result.

    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return 0;
        }
        int[] sum = new int[triangle.size() + 1];
        for (int i = triangle.size() - 1; i >= 0; i--) {
            for (int j = 0; j < triangle.get(i).size(); j++) {
                sum[j] = Math.min(sum[j], sum[j + 1]) + triangle.get(i).get(j);
            }
        }
        return sum[0];
    }

  • 0
    C

    I believe your solution is O(n^2) in time.


  • 0
    C

    All right. It's O(n^2). Thanks for your reminder. I will fix it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.