728x90
my full code
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool comp(vector<float> f1, vector<float> f2){
if(f1[1] > f2[1]) return true;
else if(f1[1] == f2[1] && f1[0] < f2[0]) return true;
return false;
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
vector<vector<float>> result;
for(int i=1; i<=N; i++){
int bottom = 0;
int top = 0;
for(auto s: stages){
if(s == i) top++;
if(s >= i) bottom++;
}
if(bottom != 0) result.push_back({(float)i, (float)top/(float)bottom});
else result.push_back({(float)i, 0});
}
sort(result.begin(), result.end(), comp);
for(auto r: result){
answer.push_back(r[0]);
}
return answer;
}
문제
https://programmers.co.kr/learn/courses/30/lessons/42889
코딩테스트 연습 - 실패율
실패율 슈퍼 게임 개발자 오렐리는 큰 고민에 빠졌다. 그녀가 만든 프랜즈 오천성이 대성공을 거뒀지만, 요즘 신규 사용자의 수가 급감한 것이다. 원인은 신규 사용자와 기존 사용자 사이에 스
programmers.co.kr
728x90
[접근]
divide by zero 의 경우를 주의하자!!
[접근1]
일단 각 스테이지에 해당하는 분자(실패 수, 해당 스테이지에 머물러있으면 실패), 분모(도달한 수, 해당 스테이지에 머물러있거나, 이미 클리어한 것)을 구하여, 스테이지의 번호와 실패율을 같이 vector에 집어넣어준다.
이때, bottom이 0이 되면, 즉 스테이지에 도달한 유저가 없는 경우 해당 스테이지의 실패율은 0이라고 제한사항에 적혀있음을 주의하자!!!
vector<int> answer;
vector<vector<float>> result;
for(int i=1; i<=N; i++){
int bottom = 0;
int top = 0;
for(auto s: stages){
if(s == i) top++;
if(s >= i) bottom++;
}
if(bottom != 0) result.push_back({(float)i, (float)top/(float)bottom});
else result.push_back({(float)i, 0});
}
[접근2]
실패율이 큰 순으로 정렬하는데, 만약 같으면 스테이지 수가 더 작은것이 우선이다.
정렬한 것을 answer에 스테이지 번호만 입력해준다.
sort(result.begin(), result.end(), comp);
for(auto r: result){
answer.push_back(r[0]);
}728x90
'coding > algorithm' 카테고리의 다른 글
| [SW Expert Academy] D1 2072. 홀수만 더하기(c++) (0) | 2022.05.18 |
|---|---|
| [SW Expert Academy] D3 1206. View(c++) (0) | 2022.05.18 |
| [programmers] Lv.2 단체사진 찍기(c++) (0) | 2022.05.07 |
| [programmers] Lv.2 카카오프렌즈 컬러링북(c++) (0) | 2022.05.06 |
| [programmers] Lv.3 징검다리 건너기(c++) (0) | 2022.05.05 |
댓글