카카오 - 실패율

아따맘마·2021년 1월 29일
0

알고리즘 - 카카오

목록 보기
19/19

문제

카카오 프로그래머스

풀이

이 문제에서 주의해야할 것은 남은 인원으로 나눠줄 때 인원이 0일 때를 고려해야 한다.

코드

#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

bool ft_compare(pair<int, double> a, pair<int, double> b)
{
	if (a.second == b.second)
		return a.first < b.first;
	return a.second > b.second;
}

vector<int> solution(int N, vector<int> stages) {
	vector<int> answer;
	vector<pair<int, double>> tmp;
	int people = stages.size();
	map<int, int> st;
	double error;

	for (int i = 0; i < stages.size(); i++)
		st[stages[i]]++;
	
	int opt = 0;
	for (int i=1;i<=N;i++)
	{
		people -= opt;
		if (people == 0)
			error = 0;
		else
			error = (double)st[i] / (double)people;
		opt = st[i];
		tmp.push_back(make_pair(i, error));
	}
	sort(tmp.begin(), tmp.end(), ft_compare);
	for (int i = 0; i < tmp.size(); i++)
		answer.push_back(tmp[i].first);

	return answer;
}

int main()
{
	vector<int> b = { 4, 4, 4, 4 };
	vector<int> ans = solution(4, b);
	for (auto x : ans)
		cout << x << ' ';
}
profile
늦게 출발했지만 꾸준히 달려서 도착지점에 무사히 도달하자

0개의 댓글