본문 바로가기
개발/코딩

해커랭크(HackerRank) - Counting Sort 1,2 / C++

by lucidmaj7 2020. 7. 11.
728x90
반응형

문제: 해커랭크 - Counting Sort 1,2

www.hackerrank.com/challenges/countingsort1/problem

 

Counting Sort 1 | HackerRank

Count the number of times each value appears.

www.hackerrank.com

언어 : C++

Counter Sort 1

// Complete the countingSort function below.
vector<int> countingSort(vector<int> arr) {
    vector<int> counter(100);

    for(int i = 0 ;i<arr.size(); i++)
    {
        counter[arr[i]] ++;
    }

    return counter;
}

 

Counter Sort 2

// Complete the countingSort function below.
vector<int> countingSort(vector<int> arr) {

    vector<int> counter(100);
    vector<int> sorted;
    for(int i = 0 ;i<arr.size(); i++)
    {
        counter[arr[i]] ++;
    }
    for(int i = 0 ;i<100; i++)
    {
        for(int j = 0;j<counter[i];j++)
        {
            sorted.push_back(i);
        }
    }
    return sorted;

}

728x90
반응형

댓글