Created
March 10, 2018 18:13
-
-
Save Nditah/efea508c55c7da5f94ae69376cbb0642 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tihube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
//check if the index from A[0]=1 to A[K] = X are covered | |
// return K | |
/* | |
1. FrogRiverOne | |
Find the earliest time when a frog can jump to the other side of a river. | |
*/ | |
function solution(X, A) { | |
let a = A.length; | |
let set = new Set(A); // remove duplicates | |
let t =-1, K=0; | |
if(set.size < X){ | |
return -1; | |
} | |
for(let i=1; i<=X ; i++){ | |
K = A.indexOf(i); | |
//console.log(i +", "+ K); | |
if(K >= 0 ){ | |
t = K > t ? K : t; | |
}else{ | |
return -1; | |
} | |
} | |
return t; | |
} | |
let X = 5, A = []; | |
A[0] = 1 | |
A[1] = 3 | |
A[2] = 1 | |
A[3] = 4 | |
A[4] = 2 | |
A[5] = 3 | |
A[6] = 5 | |
A[7] = 4 | |
console.log(solution(X, A)) ; // 6 | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> //check if the index from A[0]=1 to A[K] = X are covered | |
// return K | |
/* | |
1. FrogRiverOne | |
Find the earliest time when a frog can jump to the other side of a river. | |
*/ | |
function solution(X, A) { | |
let a = A.length; | |
let set = new Set(A); // remove duplicates | |
let t =-1, K=0; | |
if(set.size < X){ | |
return -1; | |
} | |
for(let i=1; i<=X ; i++){ | |
K = A.indexOf(i); | |
//console.log(i +", "+ K); | |
if(K >= 0 ){ | |
t = K > t ? K : t; | |
}else{ | |
return -1; | |
} | |
} | |
return t; | |
} | |
let X = 5, A = []; | |
A[0] = 1 | |
A[1] = 3 | |
A[2] = 1 | |
A[3] = 4 | |
A[4] = 2 | |
A[5] = 3 | |
A[6] = 5 | |
A[7] = 4 | |
console.log(solution(X, A)) ; // 6</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//check if the index from A[0]=1 to A[K] = X are covered | |
// return K | |
/* | |
1. FrogRiverOne | |
Find the earliest time when a frog can jump to the other side of a river. | |
*/ | |
function solution(X, A) { | |
let a = A.length; | |
let set = new Set(A); // remove duplicates | |
let t =-1, K=0; | |
if(set.size < X){ | |
return -1; | |
} | |
for(let i=1; i<=X ; i++){ | |
K = A.indexOf(i); | |
//console.log(i +", "+ K); | |
if(K >= 0 ){ | |
t = K > t ? K : t; | |
}else{ | |
return -1; | |
} | |
} | |
return t; | |
} | |
let X = 5, A = []; | |
A[0] = 1 | |
A[1] = 3 | |
A[2] = 1 | |
A[3] = 4 | |
A[4] = 2 | |
A[5] = 3 | |
A[6] = 5 | |
A[7] = 4 | |
console.log(solution(X, A)) ; // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment