[프로그래머스] 2019 KAKAO BLIND RECRUITMENT : 실패율 (C++)

김영한·2021년 9월 6일
0

알고리즘

목록 보기
70/74

문제 링크 : 실패율

[문제 접근]

  • 해당 스테이지를 도전한 사용자 인원

    • 멈춰 있는 스테이지 번호가 탐색할 스테이지보다 크거나 같은 것들의 개수
    • 전체 인원 - lower_bound의 인덱스
  • 해당 스테이지를 클리어하지 못한 사용자 인원

    • 멈춰 있는 스테이지 번호가 탐색할 스테이지와 같은 것들의 개수
    • upper_bound의 인덱스 - lower_bound의 인덱스
  • 분모가 0인 경우 예외처리

[소스 코드]

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

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

vector<int> solution(int N, vector<int> stages) {
    vector<int> answer;
    int size = stages.size();
    sort(stages.begin(), stages.end());
    vector<pair<int, double>> info;
    
    for(int i=1 ; i<=N ; i++) {
        int lw = lower_bound(stages.begin(), stages.end(), i) - stages.begin();
        int up = upper_bound(stages.begin(), stages.end(), i) - stages.begin();
        int challenger = size-lw;
        int noClear = up-lw;
        if(challenger==0) info.push_back({i, 0}); // 분모가 0일 때 예외처리
        else info.push_back({i, (double)noClear/challenger});
    }
    
    sort(info.begin(), info.end(), cmp);
    for(pair<int, double> temp : info) {
        answer.push_back(temp.first);
    }
    
    return answer;
}

0개의 댓글