Is my solution suitable for a job interview(passed all the test cases)


  • 0
    S

    As we know nCr = (sigma i= 1 to n )n-r+i/ i say 5C2= 4/1 *5/2 4= 5-2+1 5= 5-2+2 and division is not a problem as product of k consecutive numbers will be divisible by k! --- long long C part
    and since each element of pascals triangle is nC0 nC1... nCn so I decided to use NCr

    class Solution {
    public:
     long long C(int n, int r) {
        if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
        long long ans = 1;
        int i;
    
        for(i = 1; i <= r; i++) {
            ans *= n - r + i;
            ans /= i;
        }
    
        return ans;
    }
        
        vector<int> getRow(int rowIndex) {
            vector<int> v;
            long long sol;
            for(int i=0;i<=rowIndex;i++)
            {sol=C(rowIndex,i);
            v.push_back(sol);}
        
           return v; 
        }
    
        
        
    };   
    

    So ,Is this solution, suitable for job interview or not.? It takes 28ms.


Log in to reply
 

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