mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 18:10:24 +08:00
Add backtracking solution to JumpGame.
This commit is contained in:
@ -0,0 +1,17 @@
|
|||||||
|
import btJumpGame from '../btJumpGame';
|
||||||
|
|
||||||
|
describe('btJumpGame', () => {
|
||||||
|
it('should solve Jump Game problem in backtracking manner', () => {
|
||||||
|
expect(btJumpGame([1, 0])).toBeTruthy();
|
||||||
|
expect(btJumpGame([100, 0])).toBeTruthy();
|
||||||
|
expect(btJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
|
||||||
|
expect(btJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
|
||||||
|
expect(btJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
|
||||||
|
expect(btJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
|
||||||
|
|
||||||
|
expect(btJumpGame([1, 0, 1])).toBeFalsy();
|
||||||
|
expect(btJumpGame([3, 2, 1, 0, 4])).toBeFalsy();
|
||||||
|
expect(btJumpGame([0, 0, 0, 0, 0])).toBeFalsy();
|
||||||
|
expect(btJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
@ -6,6 +6,7 @@ describe('grdJumpGame', () => {
|
|||||||
expect(grdJumpGame([100, 0])).toBeTruthy();
|
expect(grdJumpGame([100, 0])).toBeTruthy();
|
||||||
expect(grdJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
|
expect(grdJumpGame([2, 3, 1, 1, 4])).toBeTruthy();
|
||||||
expect(grdJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
|
expect(grdJumpGame([1, 1, 1, 1, 1])).toBeTruthy();
|
||||||
|
expect(grdJumpGame([1, 1, 1, 10, 1])).toBeTruthy();
|
||||||
expect(grdJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
|
expect(grdJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy();
|
||||||
|
|
||||||
expect(grdJumpGame([1, 0, 1])).toBeFalsy();
|
expect(grdJumpGame([1, 0, 1])).toBeFalsy();
|
||||||
|
42
src/algorithms/uncategorized/jump-game/btJumpGame.js
Normal file
42
src/algorithms/uncategorized/jump-game/btJumpGame.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* BACKTRACKING approach of solving Jump Game.
|
||||||
|
*
|
||||||
|
* @param {number[]} numbers - array of possible jump length.
|
||||||
|
* @param {number} startIndex - index from where we start jumping.
|
||||||
|
* @param {number[]} currentJumps - current jumps path.
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
|
export default function btJumpGame(numbers, startIndex = 0, currentJumps = []) {
|
||||||
|
if (startIndex === numbers.length - 1) {
|
||||||
|
// We've jumped directly to last cell. This situation is a solution.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check what the longest jump we could make from current position.
|
||||||
|
// We don't need to jump beyond the array.
|
||||||
|
const maxJumpLength = Math.min(
|
||||||
|
numbers[startIndex], // Jump is within array.
|
||||||
|
numbers.length - 1 - startIndex, // Jump goes beyond array.
|
||||||
|
);
|
||||||
|
|
||||||
|
// Let's start jumping from startIndex and see whether any
|
||||||
|
// jump is successful and has reached the end of the array.
|
||||||
|
for (let jumpLength = maxJumpLength; jumpLength > 0; jumpLength -= 1) {
|
||||||
|
// Try next jump.
|
||||||
|
const nextIndex = startIndex + jumpLength;
|
||||||
|
currentJumps.push(nextIndex);
|
||||||
|
|
||||||
|
const isJumpSuccessful = btJumpGame(numbers, nextIndex, currentJumps);
|
||||||
|
|
||||||
|
// Check if current jump was successful.
|
||||||
|
if (isJumpSuccessful) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// BACKTRACKING.
|
||||||
|
// If previous jump wasn't successful then retreat and try the next one.
|
||||||
|
currentJumps.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
* GREEDY approach of solving Jump Game.
|
* GREEDY approach of solving Jump Game.
|
||||||
*
|
*
|
||||||
* @param {number[]} numbers - array of possible jump length.
|
* @param {number[]} numbers - array of possible jump length.
|
||||||
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
export default function grdJumpGame(numbers) {
|
export default function grdJumpGame(numbers) {
|
||||||
// The "good" cell is a cell from which we may jump to the last cell of the numbers array.
|
// The "good" cell is a cell from which we may jump to the last cell of the numbers array.
|
||||||
|
Reference in New Issue
Block a user