120. Triangle - LeetCode Solution


Array Dynamic Programming

Python Code:

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

        dp = []

        for i in range(len(triangle) + 1):
            k = []
            for j in range(len(triangle) + 1):
                k.append(-1)
            dp.append(k)


        def trav(i, j):
            if i >= len(triangle):
                return 0

            if j >= len(triangle[i]):
                return 0

            if dp[i][j] != -1:
                return dp[i][j]

            dp[i][j] = min(trav(i + 1, j) + triangle[i][j], trav(i + 1, j + 1) + triangle[i][j])
            return dp[i][j]


        return trav(0, 0)


Comments

Submit
0 Comments
More Questions

896A - Nephren gives a riddle
761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability