Below is the implementation of the unique paths:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091/** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (2D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePaths(m, n) { // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base cases for (let i = 0; i < m; i++) { dp[i][0] = 1; // First column: only one way to reach (by moving down) } for (let j = 0; j < n; j++) { dp[0][j] = 1; // First row: only one way to reach (by moving right) } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (1D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsOptimized(m, n) { // Initialize DP array for the first row const dp = Array(n).fill(1); // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[j] = dp[j] + dp[j - 1]; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Combinatorial approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsCombinatorial(m, n) { // Calculate total number of moves const total = m + n - 2; // Choose the smaller of m-1 and n-1 as k const k = Math.min(m - 1, n - 1); // Calculate C(total, k) let result = 1; for (let i = 1; i <= k; i++) { result = result * (total - k + i) / i; } return Math.round(result); // Round to handle floating-point precision issues} // Test casesconsole.log(uniquePaths(3, 7)); // 28console.log(uniquePaths(3, 2)); // 3console.log(uniquePaths(7, 3)); // 28console.log(uniquePaths(3, 3)); // 6 console.log(uniquePathsOptimized(3, 7)); // 28console.log(uniquePathsOptimized(3, 2)); // 3console.log(uniquePathsOptimized(7, 3)); // 28console.log(uniquePathsOptimized(3, 3)); // 6 console.log(uniquePathsCombinatorial(3, 7)); // 28console.log(uniquePathsCombinatorial(3, 2)); // 3console.log(uniquePathsCombinatorial(7, 3)); // 28console.log(uniquePathsCombinatorial(3, 3)); // 6
Let's break down the implementation:
Implement the unique paths solution in different programming languages.
Below is the implementation of the unique paths in different programming languages. Select a language tab to view the corresponding code.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091/** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (2D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePaths(m, n) { // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base cases for (let i = 0; i < m; i++) { dp[i][0] = 1; // First column: only one way to reach (by moving down) } for (let j = 0; j < n; j++) { dp[0][j] = 1; // First row: only one way to reach (by moving right) } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (1D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsOptimized(m, n) { // Initialize DP array for the first row const dp = Array(n).fill(1); // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[j] = dp[j] + dp[j - 1]; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Combinatorial approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsCombinatorial(m, n) { // Calculate total number of moves const total = m + n - 2; // Choose the smaller of m-1 and n-1 as k const k = Math.min(m - 1, n - 1); // Calculate C(total, k) let result = 1; for (let i = 1; i <= k; i++) { result = result * (total - k + i) / i; } return Math.round(result); // Round to handle floating-point precision issues} // Test casesconsole.log(uniquePaths(3, 7)); // 28console.log(uniquePaths(3, 2)); // 3console.log(uniquePaths(7, 3)); // 28console.log(uniquePaths(3, 3)); // 6 console.log(uniquePathsOptimized(3, 7)); // 28console.log(uniquePathsOptimized(3, 2)); // 3console.log(uniquePathsOptimized(7, 3)); // 28console.log(uniquePathsOptimized(3, 3)); // 6 console.log(uniquePathsCombinatorial(3, 7)); // 28console.log(uniquePathsCombinatorial(3, 2)); // 3console.log(uniquePathsCombinatorial(7, 3)); // 28console.log(uniquePathsCombinatorial(3, 3)); // 6
Set up a dynamic programming array to keep track of the number of unique paths to reach each cell in the grid.
Define the base cases: there is only one way to reach any cell in the first row or first column (by moving only right or only down, respectively).
For each cell, calculate the number of unique paths to reach it by adding the number of paths to reach the cell above it and the cell to its left.
Return the value in the bottom-right corner of the DP array, which represents the number of unique paths to reach the destination.
Reduce the space complexity by using a 1D array instead of a 2D array, updating it row by row.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the unique paths problem using a different approach than shown above.
Handle the case where m or n is 1 (there is only one path).
Handle small grid sizes correctly.
Handle large grid sizes efficiently to avoid overflow or timeout issues.
Below is the implementation of the unique paths:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091/** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (2D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePaths(m, n) { // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base cases for (let i = 0; i < m; i++) { dp[i][0] = 1; // First column: only one way to reach (by moving down) } for (let j = 0; j < n; j++) { dp[0][j] = 1; // First row: only one way to reach (by moving right) } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (1D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsOptimized(m, n) { // Initialize DP array for the first row const dp = Array(n).fill(1); // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[j] = dp[j] + dp[j - 1]; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Combinatorial approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsCombinatorial(m, n) { // Calculate total number of moves const total = m + n - 2; // Choose the smaller of m-1 and n-1 as k const k = Math.min(m - 1, n - 1); // Calculate C(total, k) let result = 1; for (let i = 1; i <= k; i++) { result = result * (total - k + i) / i; } return Math.round(result); // Round to handle floating-point precision issues} // Test casesconsole.log(uniquePaths(3, 7)); // 28console.log(uniquePaths(3, 2)); // 3console.log(uniquePaths(7, 3)); // 28console.log(uniquePaths(3, 3)); // 6 console.log(uniquePathsOptimized(3, 7)); // 28console.log(uniquePathsOptimized(3, 2)); // 3console.log(uniquePathsOptimized(7, 3)); // 28console.log(uniquePathsOptimized(3, 3)); // 6 console.log(uniquePathsCombinatorial(3, 7)); // 28console.log(uniquePathsCombinatorial(3, 2)); // 3console.log(uniquePathsCombinatorial(7, 3)); // 28console.log(uniquePathsCombinatorial(3, 3)); // 6
Let's break down the implementation:
Implement the unique paths solution in different programming languages.
Below is the implementation of the unique paths in different programming languages. Select a language tab to view the corresponding code.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091/** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (2D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePaths(m, n) { // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base cases for (let i = 0; i < m; i++) { dp[i][0] = 1; // First column: only one way to reach (by moving down) } for (let j = 0; j < n; j++) { dp[0][j] = 1; // First row: only one way to reach (by moving right) } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Dynamic Programming (1D) approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsOptimized(m, n) { // Initialize DP array for the first row const dp = Array(n).fill(1); // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[j] = dp[j] + dp[j - 1]; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner. * Combinatorial approach. * * @param {number} m - Number of rows * @param {number} n - Number of columns * @return {number} - Number of unique paths */function uniquePathsCombinatorial(m, n) { // Calculate total number of moves const total = m + n - 2; // Choose the smaller of m-1 and n-1 as k const k = Math.min(m - 1, n - 1); // Calculate C(total, k) let result = 1; for (let i = 1; i <= k; i++) { result = result * (total - k + i) / i; } return Math.round(result); // Round to handle floating-point precision issues} // Test casesconsole.log(uniquePaths(3, 7)); // 28console.log(uniquePaths(3, 2)); // 3console.log(uniquePaths(7, 3)); // 28console.log(uniquePaths(3, 3)); // 6 console.log(uniquePathsOptimized(3, 7)); // 28console.log(uniquePathsOptimized(3, 2)); // 3console.log(uniquePathsOptimized(7, 3)); // 28console.log(uniquePathsOptimized(3, 3)); // 6 console.log(uniquePathsCombinatorial(3, 7)); // 28console.log(uniquePathsCombinatorial(3, 2)); // 3console.log(uniquePathsCombinatorial(7, 3)); // 28console.log(uniquePathsCombinatorial(3, 3)); // 6
Set up a dynamic programming array to keep track of the number of unique paths to reach each cell in the grid.
Define the base cases: there is only one way to reach any cell in the first row or first column (by moving only right or only down, respectively).
For each cell, calculate the number of unique paths to reach it by adding the number of paths to reach the cell above it and the cell to its left.
Return the value in the bottom-right corner of the DP array, which represents the number of unique paths to reach the destination.
Reduce the space complexity by using a 1D array instead of a 2D array, updating it row by row.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the unique paths problem using a different approach than shown above.
Handle the case where m or n is 1 (there is only one path).
Handle small grid sizes correctly.
Handle large grid sizes efficiently to avoid overflow or timeout issues.