programmers.co.kr/learn/courses/30/lessons/42889
코딩테스트 연습 - 실패율
실패율 슈퍼 게임 개발자 오렐리는 큰 고민에 빠졌다. 그녀가 만든 프랜즈 오천성이 대성공을 거뒀지만, 요즘 신규 사용자의 수가 급감한 것이다. 원인은 신규 사용자와 기존 사용자 사이에 스
programmers.co.kr
- 2019 카카오 블라인드 테스트의 가장 쉬운 문제이다
- 최근에 풀었던 문제는 아니지만 기록하기위해 업로드한다.
더보기
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#define DEBUG_CODE 0
using namespace std;
struct Answer
{
int stage;
double rate;
};
bool compare(Answer& lhs, Answer& rhs)
{
if(lhs.rate == rhs.rate)
{
return lhs.stage < rhs.stage;
}
else
{
return lhs.rate > rhs.rate;
}
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
// stage, userCount
unordered_map<int, int> arrivedUserList;
unordered_map<int, int> notClearedList;
// stage, fail rate
unordered_map<int, double> failRateList;
vector<Answer> answerList;
for(int idx = 1; idx <= N; idx++)
{
arrivedUserList[idx] = 0;
notClearedList[idx] = 0;
}
for(const int stage : stages)
{
notClearedList[stage]++;
for(int idx = 1; idx <= stage; idx++)
{
arrivedUserList[idx]++;
}
}
// calculated fail rate
for(int idx = 1; idx <= N; idx++)
{
if(0 == notClearedList[idx])
{
failRateList[idx] = 0.0;
}
else
{
failRateList[idx] = (double)notClearedList[idx] / (double)arrivedUserList[idx];
#if DEBUG_CODE
printf("%s(%d) { stage : %d, notClearedList : %d, arrivedUserList : %d }\n", __FUNCTION__, __LINE__,
idx, notClearedList[idx], arrivedUserList[idx]);
#endif
}
}
for(const auto& data : failRateList)
{
Answer ans;
ans.stage = data.first;
ans.rate = data.second;
#if DEBUG_CODE
printf("%s(%d) { stage : %d, rate : %f }\n", __FUNCTION__, __LINE__, ans.stage, ans.rate);
#endif
answerList.push_back(ans);
}
sort(answerList.begin(), answerList.end(), compare);
for(const Answer& ans : answerList)
{
answer.push_back(ans.stage);
}
return answer;
}
'Coding Problem > 프로그래머스' 카테고리의 다른 글
[프로그래머스/Lv1] - 완주하지 못한 선수 (0) | 2021.04.24 |
---|---|
[프로그래머스/Lv1/카카오] - 신규 아이디 추천 (0) | 2021.04.20 |
[프로그래머스/Lv1/카카오] - 크레인 인형뽑기 (0) | 2021.04.19 |
[프로그래머스/Lv1] - K번째 수 (0) | 2021.04.18 |
[프로그래머스/Lv1] - 문자열 내 p 와 y의 개수 (0) | 2021.04.18 |