[LeetCode][120. Triangle] Dynamic Programming: Top-Down and Bottom-Up Approach
By Long Luo
This article is the solution Dynamic Programming Space O(n) Solutions: Top-Down and Bottom-Up Approaches of Problem 120. Triangle .
Intuition
This problem is a classic and typical dynamic programming problem. We can break the large problem into sub-problems.
We can use both the Top-Down and Bottom-Up approach to solve this problem.
Top-Down Approach
- State definition:
\(dp[i][j]\) represents the minimum path sum of row \(i\) and column \(j\).
- State Analysis:
\(dp[0][0]=c[0][0]\)
- The State Transfer Equation:
\[ dp[i][j] = \begin{cases} dp[i-1][0] + c[i][0] & j=0 \\ dp[i-1][i-1] + c[i][i] & j==i \\ min(dp[i-1][j-1], dp[i-1][j]) + c[i][j] & 0 < j < i \\ \end{cases} \]
so we can easily write such code: