@@ -5,7 +5,8 @@ let debug = 0;
5
5
*
6
6
* Runtime:
7
7
* best case O(r + c)
8
- * worst case O(2^(r + c + o))
8
+ * worst case is NOT O(2^(r + c))
9
+ * BUT O(rc)
9
10
*
10
11
* @param rows
11
12
* @param columns
@@ -16,14 +17,15 @@ let debug = 0;
16
17
*/
17
18
function findRobotPath(rows, columns, obstacle = [], path = [], position = {row: 0, column:0}) {
18
19
debug++;
20
+ // console.log(position);
19
21
const lastRow = rows - 1;
20
22
const lastColumn = columns - 1;
21
23
22
24
if(position.row === lastRow && position.column === lastColumn) {
23
25
// console.log('calls', debug);
24
26
return path;
25
27
} else if(obstacle.some((o) => o.row === position.row && o.column === position.column) || position.row > lastRow || position.column > lastColumn) {
26
- // console.log('calls', debug);
28
+ // console.log('* calls', debug);
27
29
return false;
28
30
} else {
29
31
const moveRight = {row: position.row, column: position.column + 1};
@@ -32,8 +34,8 @@ function findRobotPath(rows, columns, obstacle = [], path = [], position = {row:
32
34
const moveDown = {row: position.row + 1, column: position.column};
33
35
const downPath = path.concat([moveDown]);
34
36
35
- return findRobotPath(rows, columns, obstacle, rightPath, moveRight) ||
36
- findRobotPath(rows, columns, obstacle, downPath, moveDown);
37
+ return (moveRight.column <= lastColumn && findRobotPath(rows, columns, obstacle, rightPath, moveRight) ) ||
38
+ (moveDown.row <= lastRow && findRobotPath(rows, columns, obstacle, downPath, moveDown) );
37
39
}
38
40
}
39
41
0 commit comments