Skip to content

Commit d648030

Browse files
committed
algorithm: add UniquePaths2 algorithm
This algorithm allows to count ways to reach right-bottom corner on the grid with obtacles
1 parent 0fab492 commit d648030

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Dynamic-Programming/UniquePaths2.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Unique Paths 2
3+
*
4+
* There is a robot on an `m x n` grid.
5+
* The robot is initially located at the top-left corner
6+
* The robot tries to move to the bottom-right corner.
7+
* The robot can only move either down or right at any point in time.
8+
*
9+
* Given grid with obstacles
10+
* An obstacle and space are marked as 1 or 0 respectively in grid.
11+
* A path that the robot takes cannot include any square that is an obstacle.
12+
* Return the number of possible unique paths that the robot can take to reach the bottom-right corner.
13+
*
14+
* More info: https://leetcode.com/problems/unique-paths-ii/
15+
*/
16+
17+
/**
18+
* @description Return 'rows x columns' grid with cells filled by 'filler'
19+
* @param {Number} rows Number of rows in the grid
20+
* @param {Number} columns Number of columns in the grid
21+
* @param {String | Number | Boolean} filler The value to fill cells
22+
* @returns {Object []}
23+
*/
24+
const generateMatrix = (rows, columns, filler = 0) => {
25+
const matrix = []
26+
for (let i = 0; i < rows; i += 1) {
27+
const submatrix = []
28+
for (let k = 0; k < columns; k += 1) {
29+
submatrix[k] = filler
30+
}
31+
matrix[i] = submatrix
32+
}
33+
return matrix
34+
}
35+
36+
/**
37+
* @description Return number of unique paths
38+
* @param {Object []} obstacles Obstacles grid
39+
* @returns {Number}
40+
*/
41+
const uniquePaths2 = (obstacles) => {
42+
if (!(obstacles instanceof Object)) {
43+
throw new Error('Input data must be type of Array')
44+
}
45+
// Create grid for calculating number of unique ways
46+
const rows = obstacles.length
47+
const columns = obstacles[0].length
48+
const grid = generateMatrix(rows, columns)
49+
// Fill the outermost cell with 1 b/c it has
50+
// the only way to reach neighbor
51+
for (let i = 0; i < rows; i += 1) {
52+
// If robot encounters an obstacle in these cells,
53+
// he cannot continue movind in that direction
54+
if (obstacles[i][0]) {
55+
break
56+
}
57+
grid[i][0] = 1
58+
}
59+
for (let j = 0; j < columns; j += 1) {
60+
if (obstacles[0][j]) {
61+
break
62+
}
63+
grid[0][j] = 1
64+
}
65+
// Fill the rest of grid by dynamic programming
66+
// using following reccurent formula:
67+
// K[i][j] = K[i - 1][j] + K[i][j - 1]
68+
for (let i = 1; i < rows; i += 1) {
69+
for (let j = 1; j < columns; j += 1) {
70+
if (obstacles[i][j]) {
71+
grid[i][j] = 0
72+
} else {
73+
grid[i][j] = grid[i - 1][j] + grid[i][j - 1]
74+
}
75+
}
76+
}
77+
return grid[rows - 1][columns - 1]
78+
}
79+
80+
export { uniquePaths2 }

0 commit comments

Comments
 (0)