Simple clean code using NcR and o(k) space


  • 0
    A

    class Solution
    {

    public:

    int ncr(int n,int r)
    {
        if(r>(n-r)) r=n-r;
        long long  ans=1;
        for(int i=0;i<r;i++)
        {
            ans*=(n-i);
            ans/=(i+1);
        }
        return (int)ans;
        
    }
    vector<int> getRow(int k) {
        k++;
        vector<int> ans(k);    // O(k) Space 
        if(k>0)
        {
            for(int i=0;i<k;i++)
            {
                if(i==0 || i==k) ans[i]=1;
                else ans[i]=ncr(k-1,i);
            }
        }
        return ans;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.