Skip to content

Commit df17e29

Browse files
committed
Add backtracking solution to JumpGame.
1 parent aacd779 commit df17e29

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import btJumpGame from '../btJumpGame';
2+
3+
describe('btJumpGame', () => {
4+
it('should solve Jump Game problem in backtracking manner', () => {
5+
expect(btJumpGame([1, 0])).toBeTruthy();
6+
expect(btJumpGame([100, 0])).toBeTruthy();
7+
expect(btJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
8+
expect(btJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
9+
expect(btJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
10+
expect(btJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
11+
12+
expect(btJumpGame([1, 0, 1])).toBeFalsy();
13+
expect(btJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
14+
expect(btJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
15+
expect(btJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
16+
});
17+
});

src/algorithms/uncategorized/jump-game/__test__/grdJumpGame.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('grdJumpGame', () => {
66
expect(grdJumpGame([100, 0])).toBeTruthy();
77
expect(grdJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
88
expect(grdJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
9+
expect(grdJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
910
expect(grdJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
1011

1112
expect(grdJumpGame([1, 0, 1])).toBeFalsy();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* BACKTRACKING approach of solving Jump Game.
3+
*
4+
* @param {number[]} numbers - array of possible jump length.
5+
* @param {number} startIndex - index from where we start jumping.
6+
* @param {number[]} currentJumps - current jumps path.
7+
* @return {boolean}
8+
*/
9+
export default function btJumpGame(numbers, startIndex = 0, currentJumps = []) {
10+
if (startIndex === numbers.length - 1) {
11+
// We've jumped directly to last cell. This situation is a solution.
12+
return true;
13+
}
14+
15+
// Check what the longest jump we could make from current position.
16+
// We don't need to jump beyond the array.
17+
const maxJumpLength = Math.min(
18+
numbers[startIndex], // Jump is within array.
19+
numbers.length - 1 - startIndex, // Jump goes beyond array.
20+
);
21+
22+
// Let's start jumping from startIndex and see whether any
23+
// jump is successful and has reached the end of the array.
24+
for (let jumpLength = maxJumpLength; jumpLength > 0; jumpLength -= 1) {
25+
// Try next jump.
26+
const nextIndex = startIndex + jumpLength;
27+
currentJumps.push(nextIndex);
28+
29+
const isJumpSuccessful = btJumpGame(numbers, nextIndex, currentJumps);
30+
31+
// Check if current jump was successful.
32+
if (isJumpSuccessful) {
33+
return true;
34+
}
35+
36+
// BACKTRACKING.
37+
// If previous jump wasn't successful then retreat and try the next one.
38+
currentJumps.pop();
39+
}
40+
41+
return false;
42+
}

src/algorithms/uncategorized/jump-game/grdJumpGame.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* GREEDY approach of solving Jump Game.
33
*
44
* @param {number[]} numbers - array of possible jump length.
5+
* @return {boolean}
56
*/
67
export default function grdJumpGame(numbers) {
78
// The "good" cell is a cell from which we may jump to the last cell of the numbers array.

0 commit comments

Comments
 (0)