119. Pascal's Triangle II

 

Promble

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

Input: rowIndex = 3
Output: [1,3,3,1]
Input: rowIndex = 0
Output: [1]

Input: rowIndex = 1
Output: [1,1]

Approach

Code

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        # the total cost of current step
        total_cost = [0 for _ in range(len(cost)+1)]
        total_cost[0] = cost[0]
        total_cost[1] = cost[1]
        cost.append(0)
        print(total_cost)
        print(cost)

        for i in range(2, len(cost)):
            print(i)
            total_cost[i] = min(total_cost[i-1], total_cost[i-2]) + cost[i]

        print(total_cost)
        return total_cost[-1]