← Recursion & DP

Micro-Drill #171 — Triangle min path

Recursion & DP Target: 15s

Bottom-up on triangle: each cell picks the smaller child. 1D array suffices since we overwrite left to right.

def minimumTotal(triangle):
    dp = triangle[-1][:]  # start from bottom row
    for i in range(len(triangle) - 2, -1, -1):
        for j in range(len(triangle[i])):
            dp[j] = triangle[i][j] + min(dp[j], dp[j+1])
    return dp[0]

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #170 Micro #172 →