https://velog.io/@kwt0124/count-%ED%95%A8%EC%88%98
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
//내림차순으로
bool compare(pair<int,float>a, pair<int,float>b)
{
if(a.second > b.second)
{
return true;
}
else if(a.second == b.second)
{
if(a.first < b.first)
return true;
}
return false;
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
map<int,int>m;
for(auto iter : stages)
{
m[iter]++;
}
vector<pair<int,float>>v(N);
int size = stages.size();
//1번 스테이지에서 N번 스테이지 까지 순차적으로 진행한다.
for(int i = 1; i<= N; i++)
{
if(size != 0)
v[i - 1] = {i, (float)m[i] / size};
else
v[i - 1] = {i , 0};
size -= m[i];
}
//정렬 할때 동일하면 작은 번호가 앞으로 오도록 해야 한다.
sort(v.begin(), v.end(), compare);
for(auto iter : v)
answer.push_back(iter.first);
return answer;
}
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
bool compare(pair<int,float>a, pair<int,float>b)
{
if(a.second > b.second)
return true;
else if(a.second == b.second )
{
if(a.first < b.first)
return true;
}
return false;
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
vector<pair<int, float>>v;
int total = 0;
for(int stageNum = 1; stageNum <= N; stageNum++)
{
int cnt = count(stages.begin(), stages.end(), stageNum);
//분모가 0이 되는 것을 방지하자.
if((stages.size() - total) != 0)
v.push_back({stageNum, cnt / float(stages.size() - total)});
else
v.push_back({stageNum, 0});
total += cnt;
}
sort(v.begin(), v.end(), compare);
for(auto i : v)
answer.push_back(i.first);
return answer;
}