Pascal's Triangle II Easy Recursive Solution

This question is pretty simple. If one can write the recursive code to find out the element (i,j) in Pascal's triangle. After that, we can iterate over to find each element of the required row. But we would have to use Memotization to avoid TLE

The code is:
'''
class Solution {

public List<Integer> getRow(int rowIndex) {
    List<Integer> val = new ArrayList<>();
    for(int j=1;j<=rowIndex+1;j++){
        val.add(getPascal(rowIndex+1,j, new int[rowIndex+2][rowIndex+2]));
    }
    
    return val;
}

public int getPascal(int i, int j, int[][] values){
    if(i==j || j==1){
        return 1;
    }
    
    if(values[i][j]>0){
        return values[i][j];
    }
    values[i][j]= getPascal(i-1,j-1, values)+getPascal(i-1,j,values);
    
    return values[i][j];
}

}
'''

Comments (0)