distinct hint

ÀÌ ¹®Á¦´Â µ¥ÀÌÅÍ »çÀÌÁî°¡ ÀÛ¾Æ µÎ °¡Áö ¹æ¹ý ¸ðµÎ ±¦ÂúÀº ¹æ¹ýÀÌÁö¸¸

µ¥ÀÌÅÍ Å©±â°¡ 10000 À̸é

1. O(n) À¸·Î ±¸Çö Çϱâ

#include <stdio.h>

int main()
{
   int a[1001]={0};
   int i,tmp,ans;
   int n;

   scanf("%d",&n);

   for( i = 1 ; i <= n ; i++){
      scanf("%d",&tmp);
      a[tmp] = 1; // ¹è¿­ÀÇ À妽º¸¦ ¼ö·Î »ç¿ë
   }

   ans = 0;
   for( i = 1 ; i <= 1000 ; i++){
      if ( a[i] == 1 ) {
         ans++;
      }
   }

   printf("%d\n",ans);
}

2. O(nlogn) À¸·Î ±¸Çö Çϱâ

  1. µ¥ÀÌÅ͸¦ Á¤·Ä ÇÕ´Ï´Ù.(logn ½Ã°£ º¹Àâµµ)
  2. µ¥ÀÌÅ͸¦ Çϳª¾¿ °¡Á®¿Í¼­ ¿øÇÏ´Â µ¥ÀÌÅÍ°¡ Àִ°¡¸¦ ÀÌÁø °Ë»öÀ» »ç¿ëÇÕ´Ï´Ù.(nlogn)
#include <stdio.h>

void Quick_Sort(int numbers[], int array_size)
{
   q_sort(numbers, 0, array_size - 1);
}

void q_sort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
     while ((numbers[right] >= pivot) && (left < right)) right--;
     if (left != right)
     {
       numbers[left] = numbers[right];
       left++;
     }
     while ((numbers[left] <= pivot) && (left < right)) left++;
     if (left != right)
     {
       numbers[right] = numbers[left];
       right--;
     }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot) q_sort(numbers, left, pivot-1);
   if (right > pivot) q_sort(numbers, pivot+1, right);
}

int main()
{
   int i, n, ans=0;
   int arr[1001];
   scanf("%d", &n);
   for(i=0; i < n;i++) scanf("%d", &arr[i]);
   Quick_Sort(arr, n);
   for(i=0;i < n;i++)
   {
      if(i > 0)
      {
         if(arr[i]==arr[i-1]) continue;
      }
      ans++;
   }
   printf("%d\n",ans);
}
   
*¼Ò½º Ãâó:pl0892029

3. O(n^2) À¸·Î ±¸Çö Çϱâ


#include <stdio.h>

int main()
{
   int a[1001];
   int i,j,tmp;
   int n,last;

   scanf("%d",&n);

   last = 0;
   for( i = 1 ; i <= n ;i++){
      scanf("%d",&tmp);

      for( j = 1 ; j <= last ; j++){
         if ( a[j] == tmp ) {
            break;
         }
      }

      if ( j > last ) {
         last++;
         a[last] = tmp;
      
      }
   }

   printf("%d\n",last);
}

[ȨÀ¸·Î]  [µÚ ·Î]