LeetCode - Pascal's Triangle II

Pascal's Triangle II

2014.1.8 22:58

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

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

Note:
Could you optimize your algorithm to use only O(k) extra space?

Solution1:

  Pascal's Triangle is a typical O(n^2) dynamic programming problem. Here is the recurrence relation:

    1. f[i][0] = 1, i ∈ N

    2. f[i][i] = 1, i ∈ N

    3. f[i][j] = f[i - 1][j - 1] + f[i - 1][j], i ∈ N+, j ∈ N ∩ [0, i - 1]

  From the recurrence relation we know that the calculation of current row depends only on the last row. Thus only O(k) extra space is needed.

  Time complexity is O(n^2). Space complexity is O(n).

Accepted code:

 1 // 1RE, 1AC
 2 class Solution {
 3 public:
 4     vector<int> getRow(int rowIndex) {
 5         // IMPORTANT: Please reset any member data you declared, as
 6         // the same Solution instance will be reused for each test case.
 7         
 8         int i, j;
 9         result.clear();
10         result.push_back(1);
11         for(i = 0; i < rowIndex; ++i){
12             tmp.push_back(1);
13             for(j = 1; j <= i; ++j){
14                 // tmp[j] = result[j - 1] + result[j];
15                 // 1RE here, you haven't even pushed, how do you tmp[j] anyway???
16                 tmp.push_back(result[j - 1] + result[j]);
17             }
18             tmp.push_back(1);
19             result = tmp;
20             tmp.clear();
21         }
22         
23         return result;
24     }
25 private:
26     vector<int> result, tmp;
27 };

 Solution2:

  A little mathematical derivation can help us reach O(n) time complexity. See the three relations below:

    1. f[i][j] = C(i, j);

    2. C(i, j) = C(i, j - 1) * (i - j + 1) / j;

    3. C(i, 0) = 1;

  C stands for combination, please see reference here if you need some background knowledge.

  Time complexity is O(n), space complexity is O(1). Generally mathematical formulae can help do things faster, because they change the time complexity to lower bound, at the cost of some labor on the brain. Guess that's why mathematicians thought they were smarter than computer scientists, huh? These stories do happen, especially on cryptography and informatics. Just go see "A Beautiful Mind" and read about John Nash.

  Math is important, learn it well and enjoy it if you can.

Accepted code:

 1 // 1AC, excellent
 2 class Solution {
 3 public:
 4     vector<int> getRow(int rowIndex) {
 5         result.clear();
 6         
 7         if(rowIndex < 0){
 8             return result;
 9         }
10         
11         long long int tmp;
12         
13         tmp = 1;
14         result.push_back((int)tmp);
15         for(int i = 1; i <= rowIndex; ++i){
16             tmp = tmp * (rowIndex - i + 1) / i;
17             result.push_back((int)tmp);
18         }
19         
20         return result;
21     }
22 private:
23     vector<int> result;
24 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3511485.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值