Pascal’s Triangle

Key Idea

Solution

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        # Understanding:
            # Input numRows = 5
            # Output [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
        # For each of the rows you iterate and generate
        output = [[1]]
        # Iterate the "rows"
        for i in range(1, numRows):
            # Set previous array
            previous = output[i-1]
            # Start current array
            current = [1]
            # Iterate index of current array
            for j in range(1, len(previous)):
                # Add the number
                current.append(previous[j-1] + previous[j])
            current.append(1)
            output.append(current)
        return output

Complexity