Friday, September 8th

Pascal's triangle

Given an integer n, return the first n rows of Pascal's triangle 


I was able to solve this in 2 different ways: iterative and recursive

For the iterative, we begin by declaring the answer vector and setting the base case when numrows=1.

vector<vector<int>> generate(int numRows) {
vector<vector<int> > ans;
vector<int> one;
one.push_back(1);
ans.push_back(one);
if(numRows==1) return ans;
        ...
}

Beyond that, we iterate through rows 1 at a time, calculating new values from the previous row.  For each row beyond row 1 (indexed at 0), we get the previous row, then create a new row and push 1 to the front.  

    for(int i=1; i<numRows; i++) {
        vector<int> row, prevrow;
        row.push_back(1);
        prevrow=ans[i-1];
        ...
    }

Then we new values in the new row by adding elements from the previous row

    for(int i=1; i<prevrow.size(); i++) {
        row.push_back(prevrow[i-1]+prevrow[i]);
    }

 Complete the new array by pushing 1 to the end, then push the new row onto the answer vector

    for(int i=1; i<numRows; i++) {
        ...
        row.push_back(1);
        ans.push_back(row);
    }

Finally, for the iterative approach, return the answer vector

    return ans;

Full iterative solution below

    vector<vector<int>> generate(int numRows) {
vector<vector<int> > ans;
vector<int> one;
one.push_back(1);
ans.push_back(one);
if(numRows==1) return ans;

for(int i=1; i<numRows; i++) {
vector<int> row, prevrow;
row.push_back(1);
prevrow=ans[i-1];
for(int i=1; i<prevrow.size(); i++) {
row.push_back(prevrow[i-1]+prevrow[i]);
}
row.push_back(1);
ans.push_back(row);
}
return ans;
}

A second approach to this problem is solving it recursively

First we declare a global variable that will store the answer.  Then, we declare a recursive function that takes a value rownum that calculates the row for number rownum.

    vector<vector<int> >ans; //global var

    void recurse(int rownum) {
        ...    
    }

For the recursive function, we define the base case as the first row, of row number 0, creates a vector with a sole 1 and pushes it to the solution array

    if(rownum==0) {
        vector<int> one;
        one.push_back(1);
        ans.push_back(one);
        return;
    }

Otherwise, the function recursively calls itself for rows one less than current

    recurse(rownum-1);

Once that function returns, we build a new row using the previous row.  We call both these rows

    vector<int> prevrow = ans[rownum-1];
    vector<int> row;

Then we build the current row by pushing a 1, calculating middle values based off the previous row, then pushing on a final 1

    row.push_back(1);
    for(int i=1; i<prevrow.size(); i++) {
        row.push_back( prevrow[i-1]+prevrow[i] );
    }
    row.push_back(1);

To complete the recursive function, we push the current row onto the solution vector

    ans.push_back(row);
    return;

Finally, in the main function, call the recursive function and then return the solution vector

    vector<vector<int>> generate(int numRows) { 
        recurse(numRows-1);
        return ans;
    }

Full recursive solution below 

    vector<vector<int> >ans; //global var

    void recurse(int rownum) {
        if(rownum==0) {
            vector<int> one;
            one.push_back(1);
            ans.push_back(one);
            return;
        }

        recurse(rownum-1);

        vector<int> prevrow = ans[rownum-1];
        vector<int> row;

        row.push_back(1);
        for(int i=1; i<prevrow.size(); i++) {
            row.push_back( prevrow[i-1]+prevrow[i] );
        }
        row.push_back(1);
        ans.push_back(row);

        return;
    }

    vector<vector<int>> generate(int numRows) {
        
        recurse(numRows-1);
        return ans;

    }

Pretty easy problem.  They call it dynamic programming but it doesn't seem like it when you have to return the entire collection of rows.  Pleased I could solve it recursively in addition to the more intuitive iterative approach.  Did some research into binomial coefficients and Pascal's triangle, got refreshed in equations for combinations and permutations.  Looked into the beta function in C++ for generating coefficients

Comments

Popular posts from this blog

Wednesday, September 20th

Monday, September 11th

Tuesday, September 19th