알고리즘/programmers

[Programmers] 실패율

꾸준함. 2021. 11. 20. 23:29

문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/42889

 

코딩테스트 연습 - 실패율

실패율 슈퍼 게임 개발자 오렐리는 큰 고민에 빠졌다. 그녀가 만든 프랜즈 오천성이 대성공을 거뒀지만, 요즘 신규 사용자의 수가 급감한 것이다. 원인은 신규 사용자와 기존 사용자 사이에 스

programmers.co.kr

간단한 구현 문제였습니다.

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 500 + 1;
pair<int, int> statistics[MAX];
bool cmp(pair<double, int> a, pair<double, int> b)
{
if (a.first > b.first)
{
return true;
}
if (a.first == b.first)
{
if (a.second < b.second)
{
return true;
}
}
return false;
}
vector<int> solution(int N, vector<int> stages) {
for (int stage : stages)
{
for (int i = 1; i <= stage; i++)
{
statistics[i].second++;
}
statistics[stage].first++;
}
vector<pair<double, int>> failureRates;
for (int i = 1; i <= N; i++)
{
if (statistics[i].first == 0)
{
failureRates.push_back({ 0, i });
}
else
{
failureRates.push_back({ double(statistics[i].first) / double(statistics[i].second), i });
}
}
sort(failureRates.begin(), failureRates.end(), cmp);
vector<int> answer;
for (pair<double, int> ratio : failureRates)
{
answer.push_back(ratio.second);
}
return answer;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

 

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형