Created
December 1, 2015 05:50
-
-
Save divmgl/05f3bc7205a2f6c71a8f to your computer and use it in GitHub Desktop.
FrogRiverOne JavaScript solution 100%/100%
This file contains 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
function solution(X, A) { | |
if (A.length === 1) { // If the array is one element | |
// And if its first item is 1 as well as the value to search for, | |
// the frog doesn't need to move | |
if (A[0] === 1 && X === 1) return 0; | |
// If not, it's impossible to go anywhere | |
else return -1; | |
} | |
var i = -1, // Counter | |
sum = 0, // Comparison sum | |
Y = (X * (X+1)) / 2, // Sum of 1..X | |
f = []; // Found elements | |
do { // Start searching | |
i++; // Increase the counter | |
// If we've already found the element, continue | |
if (f[A[i]]) continue; | |
// If we haven't found the element, mark it | |
f[A[i]] = true; | |
// Add to the comparison sum that we will be using | |
// to determine whether the frog will have been | |
// able to cross successfully by this point and then | |
// compare it | |
sum += A[i]; | |
if (sum === Y) break; | |
} while (i < A.length) // If the counter is over the length, we didn't find it | |
// If we reached this point and this conditional is true, | |
// we didn't find the number. | |
if (i === A.length) return -1; | |
return i; // Return how long it took | |
} |
Just a few improvements. You could shorten it even more decreasing the sum variable until you reached 0 (that would be your Y variable), and also no counter is needed:
function solution(X, A) {
//get max
const min = Math.min(...A);
const max = X;
if (min === max && max > 1) {
return -1;
}
//get what total sum should be with n * (n+1)/2
let totalPossibleSum = (max * (max + 1)) / 2;
//use a map to substract numbers and use a counter to see if all the leafes are on the water
const map = {};
for (let i = 0; i < A.length; i++) {
//finish condition
if (!map[A[i]]) {
map[A[i]] = true;
totalPossibleSum -= A[i];
if (totalPossibleSum === 0) {
return i;
}
}
}
return -1;
}
Actually, just keeping track of the number of leaves will suffice.
const solution = (X, A) => {
const leavesArray = [];
let leafCount = X;
if (A && Array.isArray(A)) {
for (let i = 0; i < A.length; i++) {
const leafNum = A[i];
if (leavesArray[leafNum] == null) {
leavesArray[leafNum] = true;
leafCount--;
if (leafCount === 0) {
return i;
}
}
}
}
return -1;
};
Easier than all:
function solution(X,A) {
const sol = [];
const counts = {};
let pos = -1;
for (let i = 0; i < A.length; i++) {
if (!counts[A[i]]) {
counts[A[i]] = true;
sol.push(counts[i]);
}
if (sol.length === X) {
pos = i;
break;
}
}
return pos;
}
This was my solution
function solution(X, A) {
const map = {}
let sum = X*(X+1)/2
for(let i = 0; i< A.length; i++){
if( (X >= A[i]) && !map[A[i]] ){
sum -= A[i]
map[A[i]] = A[i]
}
if(sum === 0) return i
}
return -1
}
This was my solution:
function solution(X, A) {
const leavesSet = new Set();
if (A.length === 1) {
if (A[0] === 1 && X === 1) return 0;
else return -1;
}
for(let i=0; i< A.length; i++) {
leavesSet.add(A[i]);
if(leavesSet.size === X) {
return i;
}
}
return -1;
}
function solution(X, A) {
const m = {}
let len = 0
for(let i=0;i<A.length;i++){
if(X >= A[i] && m[A[i]]==null) {
m[A[i]] = 1
len++
}
if(len == X)
return i
}
return -1
}
function solution(X, A) {
let sum = (X * (X + 1)) / 2;
const hash = {};
for (let i = 0; i < A.length; i++) {
const element = A[i];
if (hash[element]) continue;
else {
hash[element] = true;
sum -= element;
if (sum === 0) return i;
}
}
return -1;
}
Here's mine.
function solution(target, array) {
const positionDict = new Set();
let minSec = 0;
for (let second=0; second <= array.length - 1; second ++){
const position = array[second];
if (positionDict.has(position)){
continue;
}
minSec = Math.max(minSec, second);
positionDict.add(position)
}
return positionDict.size === target ? minSec : -1;
}
Here's my solution, using the findIndex method which already satisfies the -1 request when no solution is found.
function solution(X, A){
const set = new Set()
return A.findIndex((el, i) => {
set.add(el)
return set.size === X
})
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great solution! So quick too!
In the event that all steps where completed on the last number in the array wouldn't this return -1.
if (i === A.length) return -1;
Should we recheck and make sure the sums are not equal as well in this statement? I don't think they ever had that in a test so it doesn't matter. Just an observation.