
class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]

        Time complexity: O(n^2).
        Space complexity: O(n^2).
        """
        if numRows == 0:
            return []

        triangle = [[1] * (r + 1) for r in range(numRows)]
        if numRows <= 2:
            return triangle
        for r in range(2, numRows):
            last_row = triangle[r - 1]
            current_row = triangle[r]
            for i in range(1, r):
                current_row[i] = last_row[i - 1] + last_row[i]
        return triangle


def main():
    numRows = 5
    print('Pascal\'s triangle:\n{}'.format(
        Solution().generate(numRows)))


if __name__ == '__main__':
    main()


class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]

        Time complexity: O(n^2).
        Space complexity: O(n).
        """
        if numRows == 0:
            return []

        # Create base of Pascal triangle.
        triangle = [[1] * (r + 1) for r in range(numRows)]

        if numRows <= 2:
            return triangle

        for r in range(2, numRows):
            last_row = triangle[r - 1]
            current_row = triangle[r]

            # In middle of current row, sum last row's two numbers.
            for i in range(1, r):
                current_row[i] = last_row[i - 1] + last_row[i]

        return triangle


def main():
    numRows = 5
    print('Pascal\'s triangle:\n{}'.format(
        Solution().generate(numRows)))


if __name__ == '__main__':
    main()

Add comments & revise space complexity
Add comments & revise space complexity

  
  class Solution(object):
      def generate(self, numRows):
          """
          :type numRows: int
          :rtype: List[List[int]]
  
          Time complexity: O(n^2).
-         Space complexity: O(n^2).
+         Space complexity: O(n).
          """
          if numRows == 0:
              return []
  
+         # Create base of Pascal triangle.
          triangle = [[1] * (r + 1) for r in range(numRows)]
+ 
          if numRows <= 2:
              return triangle
+ 
          for r in range(2, numRows):
              last_row = triangle[r - 1]
              current_row = triangle[r]
+ 
+             # In middle of current row, sum last row's two numbers.
              for i in range(1, r):
                  current_row[i] = last_row[i - 1] + last_row[i]
+ 
          return triangle
  
  
  def main():
      numRows = 5
      print('Pascal\'s triangle:\n{}'.format(
          Solution().generate(numRows)))
  
  
  if __name__ == '__main__':
      main()
  
Add comments & revise space complexity
## Code Before:

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]

        Time complexity: O(n^2).
        Space complexity: O(n^2).
        """
        if numRows == 0:
            return []

        triangle = [[1] * (r + 1) for r in range(numRows)]
        if numRows <= 2:
            return triangle
        for r in range(2, numRows):
            last_row = triangle[r - 1]
            current_row = triangle[r]
            for i in range(1, r):
                current_row[i] = last_row[i - 1] + last_row[i]
        return triangle


def main():
    numRows = 5
    print('Pascal\'s triangle:\n{}'.format(
        Solution().generate(numRows)))


if __name__ == '__main__':
    main()

## Instruction:
Add comments & revise space complexity
## Code After:

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]

        Time complexity: O(n^2).
        Space complexity: O(n).
        """
        if numRows == 0:
            return []

        # Create base of Pascal triangle.
        triangle = [[1] * (r + 1) for r in range(numRows)]

        if numRows <= 2:
            return triangle

        for r in range(2, numRows):
            last_row = triangle[r - 1]
            current_row = triangle[r]

            # In middle of current row, sum last row's two numbers.
            for i in range(1, r):
                current_row[i] = last_row[i - 1] + last_row[i]

        return triangle


def main():
    numRows = 5
    print('Pascal\'s triangle:\n{}'.format(
        Solution().generate(numRows)))


if __name__ == '__main__':
    main()
