Codility 'FrogRiverOne' Solution | MartinKysel.com
Find the earliest time when a frog can jump to the other side of a river.
http://codility-lessons.blogspot.com/2014/07/lesson-2-frogriverone.html
int solution(int X, int A[], int N)
{
//the array to store flags
int B[X];
//clear the flags.
memset(B, 0x00, sizeof(B[0]) * X);
//let leaves fall!
int t;
int cnt = 0;
int ans = -1;
for (t = 0; t < N; t++){
//get the position where a leave fall at time T.
int p = A[t] - 1; //(the array B is from 0 to X-1)
//if it is between 0 to X-1 and
//no leave has fallen onto the position before, count it.
if (p < X && B[p] == 0){
B[p] = 1;
cnt++;
//is the route filled with leaves?
if (cnt == X){
ans = t;
break;
}
}
}
return ans;
}
http://yuanhsh.iteye.com/blog/2173851
This algorithm only requires O(n) time, since it always keeps track of how many "holes" we still have to fill with leaves.
todo is the number of leaves still required to build the "bridge" of leaves. Whenever a leaf falls down, we first check whether there not already is a leaf at the position it falls down. If not, we decrement todo, fill the hole and go on. As soon as todo reaches 0, the entire river is covered ;)
//???
Find the earliest time when a frog can jump to the other side of a river.
A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river.
You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in seconds.
The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X. You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river.
For example, you are given integer X = 5 and array A such that:
A[0] = 1 A[1] = 3 A[2] = 1 A[3] = 4 A[4] = 2 A[5] = 3 A[6] = 5 A[7] = 4
In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river.
Write a function:
int solution(int X, int A[], int N);
that, given a non-empty zero-indexed array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.
If the frog is never able to jump to the other side of the river, the function should return −1.
http://codesays.com/2014/solution-to-frog-river-one-by-codility/2
3
4
5
6
7
8
9
10
11
12
13
14
| def solution(X, A): passable = [ False ] * X uncovered = X for idx in xrange ( len (A)): if A[idx] < = 0 or A[idx] > X: raise Exception( "Invalid value" , A[idx]) if passable[A[idx] - 1 ] = = False : passable[A[idx] - 1 ] = True uncovered - = 1 if uncovered = = 0 : return idx return - 1 |
int solution(int X, int A[], int N)
{
//the array to store flags
int B[X];
//clear the flags.
memset(B, 0x00, sizeof(B[0]) * X);
//let leaves fall!
int t;
int cnt = 0;
int ans = -1;
for (t = 0; t < N; t++){
//get the position where a leave fall at time T.
int p = A[t] - 1; //(the array B is from 0 to X-1)
//if it is between 0 to X-1 and
//no leave has fallen onto the position before, count it.
if (p < X && B[p] == 0){
B[p] = 1;
cnt++;
//is the route filled with leaves?
if (cnt == X){
ans = t;
break;
}
}
}
return ans;
}
http://yuanhsh.iteye.com/blog/2173851
This algorithm only requires O(n) time, since it always keeps track of how many "holes" we still have to fill with leaves.
todo is the number of leaves still required to build the "bridge" of leaves. Whenever a leaf falls down, we first check whether there not already is a leaf at the position it falls down. If not, we decrement todo, fill the hole and go on. As soon as todo reaches 0, the entire river is covered ;)
//???
- public int solution(int X, int[] A) {
- boolean[] tiles = new boolean[X];
- int todo = X;
- for (int i = 0; i < A.length; i++) {
- int internalIndex = A[i] - 1;
- if (!tiles[internalIndex]) {
- todo--;
- tiles[internalIndex] = true;
- }
- if (todo == 0)
- return i;
- }
- return -1;
- }