Pascal's Triangle II

Problem Id: 119 Difficulty: Easy Tag: Array


Intuition

The kth row have k + 1 values.

Since the value of nums[i][j] = nums[i - 1][j - 1] + nums[i - 1][j], so we can't change j - 1 before we use j. So we update the array from right to left.

Solution


class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        row = [1] * (rowIndex + 1)
        for i in range(2, rowIndex + 1):
            for j in range(i - 1, 0, -1):
                row[j] += row[j - 1]
        return row