Finding all the unique paths in JavaScript

In a grid navigation problem, we need to find the number of unique paths from the top-left corner (0,0) to the bottom-right corner (m-1, n-1) of an m×n grid, where movement is restricted to only right or down directions.

This is a classic dynamic programming problem. Each cell's value represents the number of ways to reach that position from the starting point.

Algorithm Explanation

The solution uses dynamic programming with these key principles:

  • First row and first column have only 1 path each (straight line)
  • For any other cell, paths = paths from above + paths from left
  • Build the solution bottom-up using a 2D array
1 1 1 1 1 2 3 4 1 3 6 10 3×4 Grid - Unique Paths Red: Base cases, Blue: Calculated, Green: Final answer

Implementation

const height = 3;
const width = 4;

const findUniquePath = (width = 1, height = 1) => {
    // Create 2D array filled with zeros
    const board = Array(height).fill(null).map(() => {
        return Array(width).fill(0);
    });
    
    // Initialize first row and first column with 1
    // (only one way to reach any cell in first row/column)
    for (let rowIndex = 0; rowIndex < height; rowIndex += 1) {
        for (let columnIndex = 0; columnIndex < width; columnIndex += 1) {
            if (rowIndex === 0 || columnIndex === 0) {
                board[rowIndex][columnIndex] = 1;
            }
        }
    }
    
    // Fill remaining cells using dynamic programming
    // Each cell = sum of paths from top + paths from left
    for (let rowIndex = 1; rowIndex < height; rowIndex += 1) {
        for (let columnIndex = 1; columnIndex < width; columnIndex += 1) {
            const uniquesFromTop = board[rowIndex - 1][columnIndex];
            const uniquesFromLeft = board[rowIndex][columnIndex - 1];
            board[rowIndex][columnIndex] = uniquesFromTop + uniquesFromLeft;
        }
    }
    
    // Return bottom-right corner value
    return board[height - 1][width - 1];
};

console.log(`Unique paths in ${height}x${width} grid:`, findUniquePath(width, height));

// Test with different grid sizes
console.log(`Unique paths in 2x2 grid:`, findUniquePath(2, 2));
console.log(`Unique paths in 5x3 grid:`, findUniquePath(5, 3));
Unique paths in 3x4 grid: 10
Unique paths in 2x2 grid: 2
Unique paths in 5x3 grid: 15

Optimized Space Solution

For large grids, we can optimize space complexity from O(m×n) to O(min(m,n)) by using only one row:

const findUniquePathOptimized = (width, height) => {
    // Use only one row to save space
    let row = Array(width).fill(1);
    
    for (let i = 1; i < height; i++) {
        for (let j = 1; j < width; j++) {
            row[j] += row[j - 1];
        }
    }
    
    return row[width - 1];
};

console.log("Optimized solution:", findUniquePathOptimized(4, 3));
Optimized solution: 10

Time and Space Complexity

Approach Time Complexity Space Complexity
2D Array (Standard) O(m × n) O(m × n)
1D Array (Optimized) O(m × n) O(min(m, n))

Conclusion

The unique paths problem demonstrates dynamic programming principles effectively. The standard solution builds a 2D table, while the optimized version uses space-efficient single-row approach. Both solutions have the same time complexity but different space requirements.

Updated on: 2026-03-15T23:19:00+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements