zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    使用fiddler2抓取手机发出的请求信息
    HTML转义字符集合
    spm3安装和使用
    JSP
    Servlet
    Struts2
    java多线程-消费者和生产者模式
    java异常处理机制(try-catch-finally)
    java内部类
    java上转型和下转型(对象的多态性)
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3511485.html
Copyright © 2011-2022 走看看