coding_test/programmers

lv1 / 실패율 / 카카오 기출 / C++

CodeJin 2022. 8. 3. 22:02

https://school.programmers.co.kr/learn/courses/30/lessons/42889

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


그냥 단순히, 모든 실패율을 다 구하고, 그 실패율을 통해 정렬하면 되는 문제. 다만, 실패율이 같으면 작은 순서가 유지되어야 해서, 안정정렬을 사용했다.

 

#include <bits/stdc++.h>
using namespace std;

vector<int> solution(int N, vector<int> stages) {
    vector<int> answer;
    vector<pair<int, double>> ratio(N);
    int l = 0, r = 0, prev;

    sort(stages.begin(), stages.end());
    for (int i = 1; i <= N; i++) {
        prev = l;
        ratio[i - 1].first = i;

        while (stages[l] <= i) l++;

        ratio[i - 1].second = (double)(l - prev) / (stages.size() - prev);
    }
    stable_sort(ratio.begin(), ratio.end(), [](const pair<int, double>& a, const pair<int, double>& b) {
        return a.second > b.second;
    });
    for (auto& x : ratio) answer.push_back(x.first);
    return answer;
}