6/25/2020

[LeetCode] 119. Pascal's Triangle II

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

Time Complexity = O ( N * N )

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

No comments:

Post a Comment