diff --git a/src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js b/src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js new file mode 100644 index 00000000..bc012b0d --- /dev/null +++ b/src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js @@ -0,0 +1,17 @@ +import backtrackingJumpGame from '../backtrackingJumpGame'; + +describe('backtrackingJumpGame', () => { + it('should solve Jump Game problem in backtracking manner', () => { + expect(backtrackingJumpGame([1, 0])).toBeTruthy(); + expect(backtrackingJumpGame([100, 0])).toBeTruthy(); + expect(backtrackingJumpGame([2, 3, 1, 1, 4])).toBeTruthy(); + expect(backtrackingJumpGame([1, 1, 1, 1, 1])).toBeTruthy(); + expect(backtrackingJumpGame([1, 1, 1, 10, 1])).toBeTruthy(); + expect(backtrackingJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy(); + + expect(backtrackingJumpGame([1, 0, 1])).toBeFalsy(); + expect(backtrackingJumpGame([3, 2, 1, 0, 4])).toBeFalsy(); + expect(backtrackingJumpGame([0, 0, 0, 0, 0])).toBeFalsy(); + expect(backtrackingJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy(); + }); +}); diff --git a/src/algorithms/uncategorized/jump-game/__test__/btJumpGame.test.js b/src/algorithms/uncategorized/jump-game/__test__/btJumpGame.test.js deleted file mode 100644 index 3a91e481..00000000 --- a/src/algorithms/uncategorized/jump-game/__test__/btJumpGame.test.js +++ /dev/null @@ -1,17 +0,0 @@ -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(); - }); -}); diff --git a/src/algorithms/uncategorized/jump-game/__test__/grdJumpGame.test.js b/src/algorithms/uncategorized/jump-game/__test__/grdJumpGame.test.js deleted file mode 100644 index 530d3641..00000000 --- a/src/algorithms/uncategorized/jump-game/__test__/grdJumpGame.test.js +++ /dev/null @@ -1,17 +0,0 @@ -import grdJumpGame from '../grdJumpGame'; - -describe('grdJumpGame', () => { - it('should solve Jump Game problem in greedy manner', () => { - expect(grdJumpGame([1, 0])).toBeTruthy(); - expect(grdJumpGame([100, 0])).toBeTruthy(); - expect(grdJumpGame([2, 3, 1, 1, 4])).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, 0, 1])).toBeFalsy(); - expect(grdJumpGame([3, 2, 1, 0, 4])).toBeFalsy(); - expect(grdJumpGame([0, 0, 0, 0, 0])).toBeFalsy(); - expect(grdJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy(); - }); -}); diff --git a/src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js b/src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js new file mode 100644 index 00000000..16962d23 --- /dev/null +++ b/src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js @@ -0,0 +1,17 @@ +import greedyJumpGame from '../greedyJumpGame'; + +describe('greedyJumpGame', () => { + it('should solve Jump Game problem in greedy manner', () => { + expect(greedyJumpGame([1, 0])).toBeTruthy(); + expect(greedyJumpGame([100, 0])).toBeTruthy(); + expect(greedyJumpGame([2, 3, 1, 1, 4])).toBeTruthy(); + expect(greedyJumpGame([1, 1, 1, 1, 1])).toBeTruthy(); + expect(greedyJumpGame([1, 1, 1, 10, 1])).toBeTruthy(); + expect(greedyJumpGame([1, 5, 2, 1, 0, 2, 0])).toBeTruthy(); + + expect(greedyJumpGame([1, 0, 1])).toBeFalsy(); + expect(greedyJumpGame([3, 2, 1, 0, 4])).toBeFalsy(); + expect(greedyJumpGame([0, 0, 0, 0, 0])).toBeFalsy(); + expect(greedyJumpGame([5, 4, 3, 2, 1, 0, 0])).toBeFalsy(); + }); +}); diff --git a/src/algorithms/uncategorized/jump-game/btJumpGame.js b/src/algorithms/uncategorized/jump-game/backtrackingJumpGame.js similarity index 87% rename from src/algorithms/uncategorized/jump-game/btJumpGame.js rename to src/algorithms/uncategorized/jump-game/backtrackingJumpGame.js index 6f791da1..843de882 100644 --- a/src/algorithms/uncategorized/jump-game/btJumpGame.js +++ b/src/algorithms/uncategorized/jump-game/backtrackingJumpGame.js @@ -6,7 +6,7 @@ * @param {number[]} currentJumps - current jumps path. * @return {boolean} */ -export default function btJumpGame(numbers, startIndex = 0, currentJumps = []) { +export default function backtrackingJumpGame(numbers, startIndex = 0, currentJumps = []) { if (startIndex === numbers.length - 1) { // We've jumped directly to last cell. This situation is a solution. return true; @@ -26,7 +26,7 @@ export default function btJumpGame(numbers, startIndex = 0, currentJumps = []) { const nextIndex = startIndex + jumpLength; currentJumps.push(nextIndex); - const isJumpSuccessful = btJumpGame(numbers, nextIndex, currentJumps); + const isJumpSuccessful = backtrackingJumpGame(numbers, nextIndex, currentJumps); // Check if current jump was successful. if (isJumpSuccessful) { diff --git a/src/algorithms/uncategorized/jump-game/grdJumpGame.js b/src/algorithms/uncategorized/jump-game/greedyJumpGame.js similarity index 95% rename from src/algorithms/uncategorized/jump-game/grdJumpGame.js rename to src/algorithms/uncategorized/jump-game/greedyJumpGame.js index 1b14eb91..b9d85a0a 100644 --- a/src/algorithms/uncategorized/jump-game/grdJumpGame.js +++ b/src/algorithms/uncategorized/jump-game/greedyJumpGame.js @@ -4,7 +4,7 @@ * @param {number[]} numbers - array of possible jump length. * @return {boolean} */ -export default function grdJumpGame(numbers) { +export default function greedyJumpGame(numbers) { // The "good" cell is a cell from which we may jump to the last cell of the numbers array. // The last cell in numbers array is for sure the "good" one since it is our goal to reach.