Pascal’s Triangle II

Key Idea

Solution

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        # Understanding
            # return the row based on pascal triangle
        
        # Set Current
        current = [1]
        # Iterate
            # If rowIndex = 0, range(1,1) > nothing
            # if rowIndex = 1, range (1,2) > iterate 1
        for i in range(1, rowIndex+1):
            previous = current
            current = [1]
            # Index of Previous Row
                # if i = 1, range(1,1) > Nothing
                # if i = 2, range(1,2) > iterate
            for j in range(1,len(previous)):
                # add
                current.append(previous[j-1] + previous[j])
            current.append(1)
        return current

Complexity