mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 01:44:52 +08:00
Add greedy solution for Jump Game.
This commit is contained in:
26
src/algorithms/uncategorized/jump-game/grdJumpGame.js
Normal file
26
src/algorithms/uncategorized/jump-game/grdJumpGame.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* GREEDY approach of solving Jump Game.
|
||||||
|
*
|
||||||
|
* @param {number[]} numbers - array of possible jump length.
|
||||||
|
*/
|
||||||
|
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 last cell in numbers array is for sure the "good" one since it is our goal to reach.
|
||||||
|
let leftGoodPosition = numbers.length - 1;
|
||||||
|
|
||||||
|
// Go through all numbers from right to left.
|
||||||
|
for (let numberIndex = numbers.length - 2; numberIndex >= 0; numberIndex -= 1) {
|
||||||
|
// If we can reach the "good" cell from the current one then for sure the current
|
||||||
|
// one is also "good". Since after all we'll be able to reach the end of the array
|
||||||
|
// from it.
|
||||||
|
const maxCurrentJumpLength = numberIndex + numbers[numberIndex];
|
||||||
|
if (maxCurrentJumpLength >= leftGoodPosition) {
|
||||||
|
leftGoodPosition = numberIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the most left "good" position is the zero's one then we may say that it IS
|
||||||
|
// possible jump to the end of the array from the first cell;
|
||||||
|
return leftGoodPosition === 0;
|
||||||
|
}
|
Reference in New Issue
Block a user