Skip to content

Instantly share code, notes, and snippets.

@matheushf
Created January 8, 2018 18:44
Show Gist options
  • Save matheushf/d1237d8d78865b328d63a96f6c632c36 to your computer and use it in GitHub Desktop.
Save matheushf/d1237d8d78865b328d63a96f6c632c36 to your computer and use it in GitHub Desktop.
Javascript solution for the Codility Frog River One
function solution(X, A) {
// write your code in JavaScript (Node.js 6.4.0)
let sequence = [0];
let position = -1;
let counter = 0;
if (X === 1 && A[0] === 1)
return 0;
for (let i = 0; i <= A.length - 1; i++) {
if (A[i] <= X) {
if (!sequence[A[i]]) {
counter++;
}
sequence[A[i]] = A[i];
if (counter === X) {
position = i;
break;
}
}
}
return position;
}
@juanlet
Copy link

juanlet commented Dec 10, 2018

You can also use X*(X+1)/2 to know how much the sum of that series will be, then you can track the positions that have leaves in an associative array where the key is array[i] and the value is a boolean set to true if it has already been looped before, and then every time a new number arrives you substract that number to the total sum X*(X+1)/2 . Then when sum is 0 you just return the i to know the minimum ammount of seconds(this would go inside the if( !positionsArray[a[i]]) in that for loop

@zachystuff
Copy link

Alternative JS Solution!
Difficult to understand the problem set because the example on codility is poor.
Idea is to go through array until you have consecutive characters up to X.
for their example,
A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 3
A[6] = 5
A[7] = 4,
you don't get 1 - 5 until 6 seconds later. Your task is to ensure a leaf has fallen on even pad leading up to X. This solution uses a set for that separation. Once the set size matches x, you know you have that many unique characters leading up to x, if you cant get that.. then function will return -1.

function solution(X, A) {

    let holdValues = new Set();
    for (i = 0; i < A.length; i++) {
        holdValues.add(A[i]);
        if (holdValues.size == X) return i;
    }

    return -1;

}

@macroramesh6
Copy link

Alternative JS Solution!
Difficult to understand the problem set because the example on codility is poor.
Idea is to go through array until you have consecutive characters up to X.
for their example,
A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 3
A[6] = 5
A[7] = 4,
you don't get 1 - 5 until 6 seconds later. Your task is to ensure a leaf has fallen on even pad leading up to X. This solution uses a set for that separation. Once the set size matches x, you know you have that many unique characters leading up to x, if you cant get that.. then function will return -1.

function solution(X, A) {

    let holdValues = new Set();
    for (i = 0; i < A.length; i++) {
        holdValues.add(A[i]);
        if (holdValues.size == X) return i;
    }

    return -1;

}

Thanks!! Ans is straight forward and simple 👍

@KamilZajac
Copy link

Alternative JS Solution!
Difficult to understand the problem set because the example on codility is poor.
Idea is to go through array until you have consecutive characters up to X.
for their example,
A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 3
A[6] = 5
A[7] = 4,
you don't get 1 - 5 until 6 seconds later. Your task is to ensure a leaf has fallen on even pad leading up to X. This solution uses a set for that separation. Once the set size matches x, you know you have that many unique characters leading up to x, if you cant get that.. then function will return -1.

function solution(X, A) {

    let holdValues = new Set();
    for (i = 0; i < A.length; i++) {
        holdValues.add(A[i]);
        if (holdValues.size == X) return i;
    }

    return -1;

}

Great solution!
Just to be precise, would add one if, to check if A[i] is smaller or equal than X - we need to cover steps from 1 to X, while now, steps like [2,3,4,5,6] will be as valid as [1,2,3,4,5], so maybe like:


    let holdValues = new Set();
    for (i = 0; i < A.length; i++) {
        if(A[i] <= X){
           holdValues.add(A[i]);
        }
        if (holdValues.size == X) return i;
    }

    return -1;

}

Anyway, it will get 100% even without this check 🥇

@Ayo-David
Copy link

@macroramesh6 solution with set makes a lot of sense and pretty easy. Thank you for sharing.

@ArielGavrielov
Copy link

ArielGavrielov commented Dec 26, 2021

let moves = [];
let leaf = 0;
for(let i = 0; i < A.length; i++) {
    if(!moves[A[i]]) {
        leaf++;
        moves[A[i]] = true;
    }
    if(leaf == X) return i;
}
return -1;

@dandok
Copy link

dandok commented May 4, 2022

I need a bit of help here if any. I have a similar solution to @KamilZajac but mine doesn't pass all cases, i'm not sure where I am missing it

const arr = Array.from(new Set([...A]))
if(!A.includes(X)) return -1;
if(arr.length === X) {
return A.indexOf(X)
}
return -1

@Odin-Taif
Copy link

Odin-Taif commented Jun 19, 2022

function solution(X, A) {

let myNum = 0;
A.sort().map(it =>(it+1) === X+1 ? myNum=it+1 : null)
return myNum;

}
I got 9% : )))) such poor score.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment