#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <iostream>
using namespace std;
// 비교함수
bool compare(pair<int, double> a, pair<int, double> b) {
if(a.second == b.second) { // 만약 실패율이 같다면
return a.first < b.first; // 스테이지 번호 작은 순 (오름차순)으로 정렬
}
else { // 실패율이 같지 않다면
return a.second > b.second; // 실패율 큰 순으로 정렬
}
}
vector<int> solution(int N, vector<int> stages) {
array<int, 502> stageUser;
fill(stageUser.begin(), stageUser.end(), 0);
// 1. 각 스테이지 별 사용자 수를 파악한다.
for(auto stage : stages) {
stageUser.at(stage)++;
}
// 2. 실패율을 구한다.
int curUser = stages.size();
vector<pair<int,double>> result;
for(int idx = 1; idx <= N; idx++) {
if(stageUser.at(idx) != 0) {
result.push_back({idx, (double)(stageUser.at(idx))/curUser});
// 다음 스테이지로 넘어갈 때 현재 스테이지에 있는 사용자 수 빼기
curUser -= stageUser.at(idx);
}
else {
// 스테이지에 사람이 없다면 실패율 0
result.push_back({idx, 0});
}
}
// 3. 실패율을 비교한다.
sort(result.begin(), result.end(), compare);
// 4. 스테이지 번호만 저장한다.
vector<int> answer;
for(auto res : result) {
answer.push_back(res.first);
}
return answer;
}
<풀이>
1. 각 스테이지 별 사용자 수를 구한다.
2. 실패율을 구한다.
3. 실패율을 비교한다.
4. 스테이지 번호만 저장한다.
'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 문자열 나누기 (C++) (0) | 2023.02.22 |
|---|---|
| [Level1] 명예의 전당 (1) (C++/Python) (0) | 2023.02.14 |
| [Level1] 가장 가까운 같은 글자 (C++/Python) (0) | 2023.02.09 |
| [Level 1] 소수 만들기 (C++/Python) (0) | 2023.02.08 |
| [Level 1] 모의고사 (C++/Python) (0) | 2023.02.07 |