søndag den 17. november 2013

The solution to the fail.

After countless of hours without sleep, I confronted my teacher about the problem at http://studenterfilosofi.blogspot.dk/2013/10/can-you-find-fail.html

You can see how I tried to complicate thing, even though it was much more simple.

    class DaCOverheadCounter
    {
       
        public static int OverheadCounter(int[] A, int L, int R)
        {
            int overHead = 0;
           
            //compare L==R????

           
            if (L < R)
            {
                //divide
                int mid = (int)Math.Floor((double)(R + L) / 2); //Remember R = A.lenght = 5 = the index. (this is zero based)

                overHead += OverheadCounter(A, L, mid); //Check left side first
               
                overHead += OverheadCounter(A, mid + 1, R); //Check right side
               
               
                //conquer
                overHead += Overhead(A, mid , mid + 1);
               
            }
         
            return overHead;
           
        }

        public static int Overhead(int[] A, int L, int R)
        {
            if (A[L] == A[R])
            {
                return 1;
            }
            return 0;
        }
    }

Ingen kommentarer:

Send en kommentar