Sunday, May 29, 2016

FrogRiverOne Codility C# solution

FrogRiverOne Codility C# solution 

Find the earliest time when a frog can jump to the other side of a river.

Solution:

public static int FrogRiverOne(int X, int[] A)
        {
            bool[] leavesInPosition = new bool[X + 1];

            int occupiedPositions = 0;
            for (int i = 0; i < A.Length; i++)
            {
                int position = A[i];
                if (position <= X && !leavesInPosition[position])
                {
                    leavesInPosition[position] = true;
                    occupiedPositions++;
                }

                if (occupiedPositions == X)
                {
                    return i;
                }
            }

            return -1;
        }

And Here goes the Result, Got 100%



Frog River One

No comments:

Post a Comment