Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Last active May 7, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iuliaL/29fe90dcd804717437993fc03e2b061b to your computer and use it in GitHub Desktop.
Save iuliaL/29fe90dcd804717437993fc03e2b061b to your computer and use it in GitHub Desktop.
function canJump(arr,d){
if (arr.length < d){
return true;
}
let pos = -1;
for (let i = 0 ; i < arr.length; i++){
if (arr[i] && i-pos <= d)
pos = i;
}
if (arr.length - pos > d)
return false;
else
return true;
}
function solution(A, D) {
// write your code in JavaScript (Node.js 6.4.0)
let t = 0;
let max = -1;
for (let i = 0; i < A.length; i++){
if (A[i] > max)
max = A[i];
}
while (t <= max){
let arr = [];
for (var i = 0; i < A.length; i++){
if (A[i]>=0 && A[i]<=t))
arr[i] = 1;
else
arr[i] = 0;
}
if (canJump(arr, D))
return t;
else {
t++;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment