pascal triangle

By | January 7, 2016
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

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

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Analysis
Matrix like below is a pascal triangle p[][]. For row i, column j, p[i][j] it has value C(i, j) [Binomial Coefficient].

pascaltriangle

Below is the formula which can help us get C(n, m) from C(n, m – 1).

pascaltriangle2

In this way, we can get elements in row i in O(n) time.

check my code on github: link

  • junmin liu

    ideally for solution like this…you won’t be able to know… Binomial Coefficient, or Catalan number and etc. you should come out your own approach, which can help you solve any problems like that.

    • http://www.allenlipeng47.com peng li

      understood. But solving by math is pretty straight forward and fastest.