😈 알고리즘/🖥️ 프로그래머스

🖥️ 최빈값 구하기 🤔

Buᐢ༝ᐢy 2022. 11. 29. 06:00

코딩테스트 연습 - 최빈값 구하기

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> array) {
     int answer = 0;
    int arrayLength = array.size();
    int maxCount[1000] = {};

    for(int i = 0; i < arrayLength; i++)
    {
        maxCount[array[i]]++;
    }

    int maxCountIndex = 0;
    int maxCountValue = 0;

    for(int i = 0; i < 1000; i++)
    {
        if(maxCount[i] > maxCountValue)
        {
            maxCountValue = maxCount[i];
            maxCountIndex = i;
        }
    }

    answer = maxCountIndex;

    for(int i = maxCountIndex + 1; i < 1000; i++)
    {
        if(maxCount[i] == maxCountValue)
        {
            answer = -1;
            break;
        }
    }

    return answer;
}

🤔 다시 풀어보기

이전에 풀었던 통계학 문제에서 코드를 그대로 썼더니 통과했다. 조금 더 효율적으로 다시 풀어봐야겠다…