Triangle
LeetCode 120 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
dp[row][col] = triangle[row][col] + min(dp[row+1][col], dp[row+1][col+1]).
நான் Triangle (LeetCode 120) ஐ இப்படித் தீர்த்தேன் — Triangle / Pascal பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
From any cell you can step to the same index or the next index in the row below. Working from the bottom up is cleanest: the best path from a cell is its value plus the better of the two cells beneath it, and the top cell ends up holding the answer.
Approach
- 1
Top-down: dfs(row, col) returns the cheapest path to the bottom; the last row returns its own value.
- 2
Bottom-up: copy the last row into dp, then fill rows upward using the two children.
- 3
dp[0][0] is the answer.
நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை
lru_cache dfs from the apex.
Fill dp from the last row upward.
Recursion + memoPythonO(n²) time · O(n²) space
from functools import lru_cache
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if not triangle:
return 0
n = len(triangle)
@lru_cache(maxsize=None)
def dfs(row,col):
if row>=n or col>=len(triangle[row]):
return float("inf")
if row==n-1:
return triangle[n-1][col]
step1 = dfs(row+1,col)
step2 = dfs(row+1,col+1)
return triangle[row][col]+min(step1,step2)
return dfs(0,0)Bottom-up tabulationPythonO(n²) time · O(n²) space
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if not triangle:
return 0
n = len(triangle)
dp= [[0]*n for _ in range(n)]
m = len(triangle[n-1])
for col in range(m):
dp[n-1][col]=triangle[n-1][col]
for row in range(n-2,-1,-1):
for col in range(len(triangle[row])):
step1 = dp[row+1][col]
step2 = float("inf")
if col + 1 < len(triangle[row + 1]):
step2 = dp[row+1][col+1]
dp[row][col]=triangle[row][col]+min(step1,step2)
return dp[0][0]
s = Solution()
##test case 1 - null triangle
assert s.minimumTotal([])==0
#test case 2 - same no of columns in next row
assert s.minimumTotal([[2],[3]])==5
#test case 3 - None triangle
assert s.minimumTotal(None)==0
#test case 4 - Single No
assert s.minimumTotal([[5]])==5Triangle — அடிக்கடி கேட்கப்படும் கேள்விகள்
Triangle பின்னாலுள்ள intuition என்ன?
From any cell you can step to the same index or the next index in the row below. Working from the bottom up is cleanest: the best path from a cell is its value plus the better of the two cells beneath it, and the top cell ends up holding the answer.
Triangle (LeetCode 120) ஐ எப்படித் தீர்ப்பது?
1. Top-down: dfs(row, col) returns the cheapest path to the bottom; the last row returns its own value. 2. Bottom-up: copy the last row into dp, then fill rows upward using the two children. 3. dp[0][0] is the answer.
Triangle இன் time மற்றும் space complexity என்ன?
Time: O(n²). Space: O(n²) (one row would do). (Accepted approach: Bottom-up tabulation.)
Triangle இல் பொதுவான தவறுகள் என்ன?
Going bottom-up removes the need to take a min over the last row at the end. Guard empty or None input.
Triangle ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?
Recursion + memo (O(n²) time · O(n²) space): lru_cache dfs from the apex. Bottom-up tabulation (O(n²) time · O(n²) space): Fill dp from the last row upward.