프로그래머스 42889 실패율

1 개요[ | ]

프로그래머스 42889 실패율

2 C++[ | ]

#include <string>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;

bool compare(const pair<float,int> &a, const pair<float,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> cnts(N+2, 0);
    for(const int& s: stages) {
        cnts[s]++;
    }
    int total = cnts[N+1];
    vector<pair<float,int>> fails;
    for(int i=N; i>=1; i--) {
        total += cnts[i];
        if(total == 0) {
            fails.push_back({0, i});    
        } else {
            fails.push_back({1.0*cnts[i]/total, i});    
        }
    }
    sort(fails.begin(), fails.end(), compare);
    vector<int> answer;
    for(const auto& f: fails) {
        answer.push_back(f.second);
    }
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}