Below is the implementation of the unique paths ii:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141/** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (2D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstacles(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for starting cell dp[0][0] = obstacleGrid[0][0] === 0 ? 1 : 0; // If starting cell is an obstacle, there are no paths if (dp[0][0] === 0) { return 0; } // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = obstacleGrid[0][j] === 0 ? dp[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = obstacleGrid[i][0] === 0 ? dp[i - 1][0] : 0; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = obstacleGrid[i][j] === 0 ? dp[i - 1][j] + dp[i][j - 1] : 0; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (1D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesOptimized(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(n).fill(0); // Set base case for starting cell dp[0] = obstacleGrid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[0][j] === 0 ? dp[j - 1] : 0; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column if (obstacleGrid[i][0] === 1) { dp[0] = 0; } // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[i][j] === 0 ? dp[j] + dp[j - 1] : 0; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * In-place Dynamic Programming approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesInPlace(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Create a copy of the grid to avoid modifying the input const grid = obstacleGrid.map(row => [...row]); // Set base case for starting cell grid[0][0] = grid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { grid[0][j] = grid[0][j] === 0 ? grid[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { grid[i][0] = grid[i][0] === 0 ? grid[i - 1][0] : 0; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] = grid[i][j] === 0 ? grid[i - 1][j] + grid[i][j - 1] : 0; } } return grid[m - 1][n - 1];} // Test casesconst obstacleGrid1 = [ [0, 0, 0], [0, 1, 0], [0, 0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesOptimized(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesInPlace(obstacleGrid1)); // 2 const obstacleGrid2 = [ [0, 1], [0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesOptimized(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesInPlace(obstacleGrid2)); // 1 const obstacleGrid3 = [ [1, 0]];console.log(uniquePathsWithObstacles(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesOptimized(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesInPlace(obstacleGrid3)); // 0
Let's break down the implementation:
Implement the unique paths ii solution in different programming languages.
Below is the implementation of the unique paths ii in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141/** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (2D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstacles(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for starting cell dp[0][0] = obstacleGrid[0][0] === 0 ? 1 : 0; // If starting cell is an obstacle, there are no paths if (dp[0][0] === 0) { return 0; } // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = obstacleGrid[0][j] === 0 ? dp[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = obstacleGrid[i][0] === 0 ? dp[i - 1][0] : 0; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = obstacleGrid[i][j] === 0 ? dp[i - 1][j] + dp[i][j - 1] : 0; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (1D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesOptimized(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(n).fill(0); // Set base case for starting cell dp[0] = obstacleGrid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[0][j] === 0 ? dp[j - 1] : 0; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column if (obstacleGrid[i][0] === 1) { dp[0] = 0; } // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[i][j] === 0 ? dp[j] + dp[j - 1] : 0; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * In-place Dynamic Programming approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesInPlace(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Create a copy of the grid to avoid modifying the input const grid = obstacleGrid.map(row => [...row]); // Set base case for starting cell grid[0][0] = grid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { grid[0][j] = grid[0][j] === 0 ? grid[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { grid[i][0] = grid[i][0] === 0 ? grid[i - 1][0] : 0; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] = grid[i][j] === 0 ? grid[i - 1][j] + grid[i][j - 1] : 0; } } return grid[m - 1][n - 1];} // Test casesconst obstacleGrid1 = [ [0, 0, 0], [0, 1, 0], [0, 0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesOptimized(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesInPlace(obstacleGrid1)); // 2 const obstacleGrid2 = [ [0, 1], [0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesOptimized(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesInPlace(obstacleGrid2)); // 1 const obstacleGrid3 = [ [1, 0]];console.log(uniquePathsWithObstacles(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesOptimized(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesInPlace(obstacleGrid3)); // 0
Set up a dynamic programming array to keep track of the number of unique paths to reach each cell in the grid.
Check if the starting cell contains an obstacle. If it does, there are no paths to the destination.
Define the base cases for the first row and first column, considering obstacles that block the path.
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, unless the cell contains an obstacle.
Return the value in the bottom-right corner of the DP array, which represents the number of unique paths to reach the destination.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the unique paths ii problem using a different approach than shown above.
Handle the case where the starting cell contains an obstacle (return 0).
Handle the case where the destination cell contains an obstacle (return 0).
Handle the case where all possible paths to the destination are blocked by obstacles.
Handle the case of a 1x1 grid.
Below is the implementation of the unique paths ii:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141/** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (2D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstacles(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for starting cell dp[0][0] = obstacleGrid[0][0] === 0 ? 1 : 0; // If starting cell is an obstacle, there are no paths if (dp[0][0] === 0) { return 0; } // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = obstacleGrid[0][j] === 0 ? dp[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = obstacleGrid[i][0] === 0 ? dp[i - 1][0] : 0; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = obstacleGrid[i][j] === 0 ? dp[i - 1][j] + dp[i][j - 1] : 0; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (1D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesOptimized(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(n).fill(0); // Set base case for starting cell dp[0] = obstacleGrid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[0][j] === 0 ? dp[j - 1] : 0; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column if (obstacleGrid[i][0] === 1) { dp[0] = 0; } // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[i][j] === 0 ? dp[j] + dp[j - 1] : 0; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * In-place Dynamic Programming approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesInPlace(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Create a copy of the grid to avoid modifying the input const grid = obstacleGrid.map(row => [...row]); // Set base case for starting cell grid[0][0] = grid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { grid[0][j] = grid[0][j] === 0 ? grid[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { grid[i][0] = grid[i][0] === 0 ? grid[i - 1][0] : 0; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] = grid[i][j] === 0 ? grid[i - 1][j] + grid[i][j - 1] : 0; } } return grid[m - 1][n - 1];} // Test casesconst obstacleGrid1 = [ [0, 0, 0], [0, 1, 0], [0, 0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesOptimized(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesInPlace(obstacleGrid1)); // 2 const obstacleGrid2 = [ [0, 1], [0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesOptimized(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesInPlace(obstacleGrid2)); // 1 const obstacleGrid3 = [ [1, 0]];console.log(uniquePathsWithObstacles(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesOptimized(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesInPlace(obstacleGrid3)); // 0
Let's break down the implementation:
Implement the unique paths ii solution in different programming languages.
Below is the implementation of the unique paths ii in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141/** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (2D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstacles(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(m).fill().map(() => Array(n).fill(0)); // Set base case for starting cell dp[0][0] = obstacleGrid[0][0] === 0 ? 1 : 0; // If starting cell is an obstacle, there are no paths if (dp[0][0] === 0) { return 0; } // Set base cases for first row for (let j = 1; j < n; j++) { dp[0][j] = obstacleGrid[0][j] === 0 ? dp[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { dp[i][0] = obstacleGrid[i][0] === 0 ? dp[i - 1][0] : 0; } // Fill DP array for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = obstacleGrid[i][j] === 0 ? dp[i - 1][j] + dp[i][j - 1] : 0; } } return dp[m - 1][n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * Dynamic Programming (1D) approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesOptimized(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Initialize DP array const dp = Array(n).fill(0); // Set base case for starting cell dp[0] = obstacleGrid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[0][j] === 0 ? dp[j - 1] : 0; } // Fill DP array for (let i = 1; i < m; i++) { // Update first column if (obstacleGrid[i][0] === 1) { dp[0] = 0; } // Update rest of the row for (let j = 1; j < n; j++) { dp[j] = obstacleGrid[i][j] === 0 ? dp[j] + dp[j - 1] : 0; } } return dp[n - 1];} /** * Calculate the number of unique paths from top-left to bottom-right corner with obstacles. * In-place Dynamic Programming approach. * * @param {number[][]} obstacleGrid - Grid with obstacles (1) and empty spaces (0) * @return {number} - Number of unique paths */function uniquePathsWithObstaclesInPlace(obstacleGrid) { const m = obstacleGrid.length; const n = obstacleGrid[0].length; // Create a copy of the grid to avoid modifying the input const grid = obstacleGrid.map(row => [...row]); // Set base case for starting cell grid[0][0] = grid[0][0] === 0 ? 1 : 0; // Set base cases for first row for (let j = 1; j < n; j++) { grid[0][j] = grid[0][j] === 0 ? grid[0][j - 1] : 0; } // Set base cases for first column for (let i = 1; i < m; i++) { grid[i][0] = grid[i][0] === 0 ? grid[i - 1][0] : 0; } // Fill grid for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] = grid[i][j] === 0 ? grid[i - 1][j] + grid[i][j - 1] : 0; } } return grid[m - 1][n - 1];} // Test casesconst obstacleGrid1 = [ [0, 0, 0], [0, 1, 0], [0, 0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesOptimized(obstacleGrid1)); // 2console.log(uniquePathsWithObstaclesInPlace(obstacleGrid1)); // 2 const obstacleGrid2 = [ [0, 1], [0, 0]];console.log(uniquePathsWithObstacles(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesOptimized(obstacleGrid2)); // 1console.log(uniquePathsWithObstaclesInPlace(obstacleGrid2)); // 1 const obstacleGrid3 = [ [1, 0]];console.log(uniquePathsWithObstacles(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesOptimized(obstacleGrid3)); // 0console.log(uniquePathsWithObstaclesInPlace(obstacleGrid3)); // 0
Set up a dynamic programming array to keep track of the number of unique paths to reach each cell in the grid.
Check if the starting cell contains an obstacle. If it does, there are no paths to the destination.
Define the base cases for the first row and first column, considering obstacles that block the path.
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, unless the cell contains an obstacle.
Return the value in the bottom-right corner of the DP array, which represents the number of unique paths to reach the destination.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the unique paths ii problem using a different approach than shown above.
Handle the case where the starting cell contains an obstacle (return 0).
Handle the case where the destination cell contains an obstacle (return 0).
Handle the case where all possible paths to the destination are blocked by obstacles.
Handle the case of a 1x1 grid.