Advertisement

Triangle - LeetCode 120 Solution

Triangle - Complete Solution Guide

Triangle is LeetCode problem 120, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 Example 2: Input: triangle = [[-10]] Output: -10 Constraints: 1 <= triangle.length <= 200 triangle[0].length == 1 triangle[i].length == triangle[i - 1].length + 1 -10^4 <

Detailed Explanation

The problem asks us to find the minimum path sum from the top to the bottom of a triangle-shaped array of numbers. From any number in the triangle, you can only move to the number directly below it or the number to the right of the number directly below it. The goal is to find the path from the top that results in the smallest sum of numbers visited.

Solution Approach

The provided solution uses a bottom-up dynamic programming approach with O(n) space complexity. It starts from the bottom row of the triangle and iterates upwards. For each row, it calculates the minimum path sum to reach each element in that row from the bottom row. The `dp` array stores the minimum path sums from each element in the current row to the bottom. By iterating upwards, the final `dp[0]` will contain the minimum path sum from the top of the triangle to the bottom.

Step-by-Step Algorithm

  1. Step 1: Initialize a `dp` array with the values from the last row of the triangle. This represents the minimum path sum from each element in the last row to itself.
  2. Step 2: Iterate upwards from the second-to-last row to the first row.
  3. Step 3: For each element in the current row, calculate the minimum path sum by adding the element's value to the minimum of the two elements directly below it in the `dp` array (i.e., `dp[j]` and `dp[j+1]`). Update `dp[j]` with this new minimum path sum.
  4. Step 4: After iterating through all rows, the value at `dp[0]` will contain the minimum path sum from the top of the triangle to the bottom. Return this value.

Key Insights

  • Insight 1: Dynamic Programming is well-suited for this problem because we can break it down into overlapping subproblems (finding the minimum path from any given point to the bottom).
  • Insight 2: Bottom-up DP is more efficient in terms of space since we only need to keep track of the minimum sums of the previous row to compute the current row's minimum sums.
  • Insight 3: We can achieve O(n) space complexity by using a single array to store the minimum path sums from the bottom row up to the current row, overwriting the previous row's values as we go.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: Agoda, DE Shaw, Salesforce.