thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

LeetCode #120: Triangle

β€’ 54 words β€’ 1 min β€’ updated

LeetCode #120: Triangle:

python
from functools import cache

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        rows = len(triangle)

        @cache
        def solve(row, col):
            assert row >= 0, col >= 0

            if row >= rows:
                return 0

            return triangle[row][col] + min(
                solve(row + 1, col),
                solve(row + 1, col + 1)
            )

        return solve(0, 0)