Tuesday, June 7, 2016

MaxCounters 100% Ala Hisoka - Codility

Berikut kode dalam bahasa Java & C# untuk memperoleh nilai 100% di training test codility MaxCounters

Bahasa Java :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  public static int[] solution100(int N, int[] A) {
         // write your code in Java SE 8
         int[] result = new int[N];
         int max = 0, save_max = 0;
         
         for(int el = 0; el<A.length; el++)
   {
    if(A[el] == N+1)
    {
     save_max = max;
    }else
    {
     if(result[A[el]-1] < save_max) result[A[el]-1] = save_max+1;
     else result[A[el]-1] += 1;
     max = result[A[el]-1]>max? result[A[el]-1]:max;
    } 
   
   }
         
         for(int l=0; l<result.length; l++)
         {
          if(result[l]<save_max) result[l] = save_max;
         }
         
   return result;
         
     }

Bahasa C# :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static int[] maxCount(int N, int[] A)
        {
            int[] result = new int[N];
            int max = 0, save_max = 0;

            for (int el = 0; el < A.Length; el++)
            {
                if (A[el] == N + 1)
                {
                    save_max = max;
                }
                else
                {
                    if (result[A[el] - 1] < save_max) result[A[el] - 1] = save_max + 1;
                    else result[A[el] - 1] += 1;
                    max = result[A[el] - 1] > max ? result[A[el] - 1] : max;
                }

            }

            for (int l = 0; l < result.Length; l++)
            {
                if (result[l] < save_max) result[l] = save_max;
            }

            return result;
        }
    }


Hasil Test untuk Java :
Hasil Test untuk C# :


No comments:

Post a Comment