KatInterview

119. Pascal's Triangle II

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to compute the rowIndex-th row of Pascal's Triangle. Pascal's Triangle has a recursive property: each number is the sum of the two numbers directly above it. The k-th element of row i is denoted by the binomial coefficient C(i, k).

Solution

Pascal's Triangle II in Python

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        # The row has rowIndex + 1 elements. Initialize with 1s.
        row = [1] * (rowIndex + 1)
        
        # Calculate each element based on the previous one.
        # Python's integers handle arbitrary size, so no overflow issues.
        for k in range(1, rowIndex + 1):
            # Formula: C(n, k) = C(n, k-1) * (n - k + 1) / k
            # We can use the previously computed value in the array directly.
            row[k] = row[k-1] * (rowIndex - k + 1) // k
            
        return row

Pascal's Triangle II in Java

class Solution {
    public List<Integer> getRow(int rowIndex) {
        // The row will have rowIndex + 1 elements.
        List<Integer> row = new ArrayList<>(rowIndex + 1);
        
        // Use long for calculations to prevent overflow.
        long val = 1;
        
        // The first element C(rowIndex, 0) is always 1.
        row.add(1);
        
        // Calculate each subsequent element using the previous one.
        for (int k = 1; k <= rowIndex; k++) {
            // Formula: C(n, k) = C(n, k-1) * (n - k + 1) / k
            val = val * (rowIndex - k + 1) / k;
            row.add((int) val);
        }
        
        return row;
    }
}

Pascal's Triangle II in C++

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        // The row will have rowIndex + 1 elements.
        vector<int> row(rowIndex + 1);
        
        // Use long long for calculations to prevent overflow.
        long long val = 1;
        
        // The first element C(rowIndex, 0) is always 1.
        row[0] = 1;
        
        // Calculate each subsequent element using the previous one.
        for (int k = 1; k <= rowIndex; k++) {
            // Formula: C(n, k) = C(n, k-1) * (n - k + 1) / k
            val = val * (rowIndex - k + 1) / k;
            row[k] = val;
        }
        
        return row;
    }
};

Pascal's Triangle II in TypeScript

function getRow(rowIndex: number): number[] {
    // The row will have rowIndex + 1 elements.
    const row: number[] = new Array(rowIndex + 1);
    
    // The first element C(rowIndex, 0) is always 1.
    row[0] = 1;
    
    // Calculate each subsequent element using the previous one.
    for (let k = 1; k <= rowIndex; k++) {
        // Formula: C(n, k) = C(n, k-1) * (n - k + 1) / k
        // JavaScript's 'number' type is a 64-bit float, which can safely
        // represent the integers involved in this problem without overflow.
        row[k] = row[k-1] * (rowIndex - k + 1) / k;
    }
    
    return row;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.