Problem statement

https://leetcode.com/problems/pascals-triangle/

Solution

In this problem we just need to use the definition of pascal’s triangle: to create next row we need to look at previoius row and compute elements. We can do it efficiently using zip functionality in python.

Complexity

It is O(n^2) both for time and space.

Code

class Solution:
    def generate(self, n):
        ans = [[1]]
        for i in range(n-1):
            ans.append([x+y for x,y in zip([0]+ans[-1], ans[-1] + [0])])     
        return ans