Skip to content

Commit cde4d94

Browse files
committed
refactor: refactor code in UniquePaths2.js
1 parent be56ea7 commit cde4d94

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

Dynamic-Programming/UniquePaths2.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
const generateMatrix = (rows, columns, filler = 0) => {
2525
const matrix = []
26-
for (let i = 0; i < rows; i += 1) {
26+
for (let i = 0; i < rows; i++) {
2727
const submatrix = []
28-
for (let k = 0; k < columns; k += 1) {
28+
for (let k = 0; k < columns; k++) {
2929
submatrix[k] = filler
3030
}
3131
matrix[i] = submatrix
@@ -35,11 +35,11 @@ const generateMatrix = (rows, columns, filler = 0) => {
3535

3636
/**
3737
* @description Return number of unique paths
38-
* @param {Object []} obstacles Obstacles grid
38+
* @param {Array [][]} obstacles Obstacles grid
3939
* @returns {Number}
4040
*/
4141
const uniquePaths2 = (obstacles) => {
42-
if (!(obstacles instanceof Object)) {
42+
if (!Array.isArray(obstacles)) {
4343
throw new Error('Input data must be type of Array')
4444
}
4545
// Create grid for calculating number of unique ways
@@ -48,15 +48,15 @@ const uniquePaths2 = (obstacles) => {
4848
const grid = generateMatrix(rows, columns)
4949
// Fill the outermost cell with 1 b/c it has
5050
// the only way to reach neighbor
51-
for (let i = 0; i < rows; i += 1) {
51+
for (let i = 0; i < rows; i++) {
5252
// If robot encounters an obstacle in these cells,
53-
// he cannot continue movind in that direction
53+
// he cannot continue moving in that direction
5454
if (obstacles[i][0]) {
5555
break
5656
}
5757
grid[i][0] = 1
5858
}
59-
for (let j = 0; j < columns; j += 1) {
59+
for (let j = 0; j < columns; j++) {
6060
if (obstacles[0][j]) {
6161
break
6262
}
@@ -65,13 +65,9 @@ const uniquePaths2 = (obstacles) => {
6565
// Fill the rest of grid by dynamic programming
6666
// using following reccurent formula:
6767
// 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-
}
68+
for (let i = 1; i < rows; i++) {
69+
for (let j = 1; j < columns; j++) {
70+
grid[i][j] = obstacles[i][j] ? 0 : grid[i - 1][j] + grid[i][j - 1]
7571
}
7672
}
7773
return grid[rows - 1][columns - 1]

Dynamic-Programming/tests/UniquePaths2.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { uniquePaths2 } from '../UniquePaths2'
22

33
describe('Unique Paths2', () => {
44
// Should return number of ways, taken into account the obstacles
5-
test('Case 1: there are obstacles in the way', () => {
5+
test('There are obstacles in the way', () => {
66
expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0]])).toEqual(2)
77
expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0]])).toEqual(3)
88
})
99
// Should return number of all possible ways to reach right-bottom corner
10-
test('Case 2: there are no obstacles in the way', () => {
10+
test('There are no obstacles in the way', () => {
1111
expect(uniquePaths2([[0, 0, 0], [0, 0, 0], [0, 0, 0]])).toEqual(6)
1212
expect(uniquePaths2([[0, 0, 0], [0, 0, 0]])).toEqual(3)
1313
})
1414
// Should throw an exception b/c input data has wrong type
15-
test('Case 3: there are wrong type of input data', () => {
15+
test('There are wrong type of input data', () => {
1616
expect(() => uniquePaths2('wrong input')).toThrow()
1717
expect(() => uniquePaths2(100)).toThrow()
1818
})

0 commit comments

Comments
 (0)