풀이 - 다시풀어보자
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(const pair<int,double> &a, const pair<int,double> &b){
if(a.second>b.second){
return true;
}else if(a.second==b.second){
if(a.first<b.first){
return true;
}else{
return false;
}
}else{
return false;
}
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
int count=stages.size();
vector <pair<int,double>> loss; //(n번 스테이지, 실패율)쌍
int j=0;
sort(stages.begin(),stages.end());
for(int i=1; i<=N; i++){ //index i번째 stage부터 차례로
if(count==0){
loss.push_back(make_pair(i,0));
continue;
}
int clear=0;
while(stages[j]==i && j<stages.size()){
clear++;
j++;
}
double rate = (double) clear/count;
loss.push_back(make_pair(i,rate));
count -= clear;
}
sort(loss.begin(), loss.end(), compare);
for(int i=0; i<N; i++){
answer.push_back(loss[i].first);
}
return answer;
}
내가 풀다 만 코드 - 조금만 더 생각하고 고치면 될거같은데..
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<int,double>a, pair<int,double>b){
if(a.first>b.first){
return true;
}else if(a.first==b.first){
if(a.second<b.second){
return true;
}else{
return false;
}
}else{
return false;
}
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
int count=stages.size();
int clear=0;
vector <pair<int,double>> loss; //(n번 스테이지, 실패율)쌍
for(int i=0; i<=N; i++){
loss.push_back(make_pair(i+1,0));
}
for(int i=0; i<=N; i++){ //index i번째 stage부터 차례로
for(int j=0; j<stages.size(); j++){ //stage 배열 개수만큼 반복문
if(loss[i].first == stages[j]){
loss[i].second += (double)(1/count);
clear++;
}
}
count -= clear;
}
sort(loss.begin(), loss.end(), compare);
for(int i=0; i<N; i++){
answer.push_back(loss[i].first);
}
return answer;
}