Hueestory

[2019 KAKAO BLIND] - 실패율 (C++) 본문

PS(중단)/programmers

[2019 KAKAO BLIND] - 실패율 (C++)

히명 2024. 8. 8. 13:28
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(pair<double, int> &a, pair<double, int> &b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first > b.first;
}

vector<int> solution(int N, vector<int> stages) {
    vector<int> answer(N);
    vector<pair<double, int>> rate;

    for (int i = 1; i <= N; i++) {
        double reach = 0, fail = 0;
        
        for (int j = 0; j < stages.size(); j++) {
            if (i <= stages[j])
                reach++;
            if (i == stages[j])
                fail++;
        }
        
        if (reach != 0)
            rate.push_back(make_pair(fail / reach, i));
        else if (reach == 0)
            rate.push_back(make_pair(0, i));
    }

    sort(rate.begin(), rate.end(), cmp);
    
    for (int i = 0; i < N; i++) {
        answer[i] = rate[i].second;
    }

    return answer;
}

 

Comments