@@ -5,7 +5,8 @@ let debug = 0;
55 *
66 * Runtime:
77 * 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)
910 *
1011 * @param rows
1112 * @param columns
@@ -16,14 +17,15 @@ let debug = 0;
1617 */
1718function findRobotPath ( rows , columns , obstacle = [ ] , path = [ ] , position = { row : 0 , column :0 } ) {
1819 debug ++ ;
20+ // console.log(position);
1921 const lastRow = rows - 1 ;
2022 const lastColumn = columns - 1 ;
2123
2224 if ( position . row === lastRow && position . column === lastColumn ) {
2325 // console.log('calls', debug);
2426 return path ;
2527 } 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);
2729 return false ;
2830 } else {
2931 const moveRight = { row : position . row , column : position . column + 1 } ;
@@ -32,8 +34,8 @@ function findRobotPath(rows, columns, obstacle = [], path = [], position = {row:
3234 const moveDown = { row : position . row + 1 , column : position . column } ;
3335 const downPath = path . concat ( [ moveDown ] ) ;
3436
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 ) ) ;
3739 }
3840}
3941
0 commit comments