O(K) time and space


#1
vector<int> Solution::getRow(int n) 
{
    if(!n)
        return {1};
    vector<int> result(n + 1);
    result[0] = result[n] = 1;
    for(int i = 1; i < n; ++i)
        result[i] = result[i - 1] * (n + 1 - i) / i;
    return result;
}

#2

How did you derive this formula?


#3

ncr formula:
for 4rth row:
elements of 4rth row will be:
4c0, 4c1, 4c2, 4c3, 4c4.