ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [HackerRank] counting sort 1 c++
    Coding Test/HackerRank 2022. 7. 20. 10:29
    728x90
    • Problem

    Quicksort usually has a running time of n x log(n), but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison sorts, i.e. they sort a list just by comparing the elements to one another. A comparison sort algorithm cannot beat n x log(n) (worst-case) running time, since n x log(n) represents the minimum number of comparisons needed to know where to place each element.

    • Example
    i	arr[i]	result
    0	1	[0, 1, 0, 0]
    1	1	[0, 2, 0, 0]
    2	3	[0, 2, 0, 1]
    3	2	[0, 2, 1, 1]
    4	1	[0, 3, 1, 1]
    • Constraints

    100<=n<=10^6

    0<=arr[i] < 100

    • Sample Input
    100
    63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33  
    • Sample Output
    0 2 0 2 0 0 1 0 1 2 1 0 1 1 0 0 2 0 1 0 1 2 1 1 1 3 0 2 0 0 2 0 3 3 1 0 0 0 0 2 2 1 1 1 2 0 2 0 1 0 1 0 0 1 0 0 2 1 0 1 1 1 0 1 0 1 0 2 1 3 2 0 0 2 1 2 1 0 2 2 1 2 1 2 1 1 2 2 0 3 2 1 1 0 1 1 1 0 2 2 

     

    • Solutions

    문제에서

    int[100]: a frequency array 를 못보고 arr size로 result vector를 초기화 시켰더니 2개만 통과하고 나머지는 통과하지 못했다.

    100으로 고정시켜서 초기화를 하니 모두 통과

    vector<int> countingSort(vector<int> arr) {
       // long long row = arr.size();
        vector<int> result(100, 0);
        for(auto &it : arr){
            result[it] += 1;
        }
        return result;
    }

     

    728x90

    'Coding Test > HackerRank' 카테고리의 다른 글

    [HackerRank] Tower Breakers c++  (0) 2022.07.20
    [HackerRank] Zig Zag Sequence C++  (0) 2022.07.20
    [HackerRank] Lonely Integer c++  (0) 2022.07.20
    [HackerRank] Between Two Sets c++  (0) 2022.07.19
    [HackerRank] Number Line Jumps c++  (0) 2022.07.19

    댓글

© 2022. code-space ALL RIGHTS RESERVED.