C++:: 프로그래머스 < 실패율 >

jahlee·2023년 7월 14일
0

프로그래머스_Lv.1

목록 보기
42/75
post-thumbnail

현재 스테이지에 도달했지만 클리어 못한 수 / 스테이지 도달한 수 를 잘계산해주어서 풀면되는 문제이다. 스테이지를 도달한 수는 이전 스테이지들에 도달한 수를전체에서 빼주면 된다.

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

bool compare(pair<int, double> a, pair<int, double> b) {
    return (a.second > b.second);
}

vector<int> solution(int N, vector<int> stages) {
    vector<int> answer;
    int players = stages.size();
    vector<pair<int, double>> status(N+1, pair<int, double>{0,0.0});
    for (int i=0; i<N+1; i++) {// 스테이지
        status[i].first = i+1;
    }
    for (auto stage : stages) {// 도달했지만 클리어 못한 수 
        status[stage-1].second += 1.0;
    }
    for (int i=0; i<N; i++) {
        int fail_player = status[i].second;
        status[i].second /= players;// 실패율로 갱신
        players -= fail_player;// 전체수에서 클리어 못한수 빼준다
    }
    status.pop_back();// N+1은 마지막 단계도 통관한것이기 때문에 빼준다.
    stable_sort(status.begin(), status.end(), compare);
    for (auto stage : status) {
        answer.push_back(stage.first);
    }
    return answer;
}

0개의 댓글