DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

Pascal's Triangle

Given an integer numRows, return the first numRows of Pascal's triangle.

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

Example 1:

Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:

Input: numRows = 1
Output: [[1]]

Constraints:

  • 1 <= numRows <= 30

SOLUTION:

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        tri = []
        curr = [1]
        n = 1
        for i in range(numRows):
            tri.append(curr)
            curr = [0] + curr
            n += 1
            for i in range(n - 1):
                curr[i] = curr[i] + curr[i + 1]
        return tri
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay