๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์‹คํŒจ์œจ( Level 1)

by ์•ˆ์ฃผํ˜• 2021. 10. 22.

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹คํŒจ์œจ

์‹คํŒจ์œจ ์Šˆํผ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž ์˜ค๋ ๋ฆฌ๋Š” ํฐ ๊ณ ๋ฏผ์— ๋น ์กŒ๋‹ค. ๊ทธ๋…€๊ฐ€ ๋งŒ๋“  ํ”„๋žœ์ฆˆ ์˜ค์ฒœ์„ฑ์ด ๋Œ€์„ฑ๊ณต์„ ๊ฑฐ๋’€์ง€๋งŒ, ์š”์ฆ˜ ์‹ ๊ทœ ์‚ฌ์šฉ์ž์˜ ์ˆ˜๊ฐ€ ๊ธ‰๊ฐํ•œ ๊ฒƒ์ด๋‹ค. ์›์ธ์€ ์‹ ๊ทœ ์‚ฌ์šฉ์ž์™€ ๊ธฐ์กด ์‚ฌ์šฉ์ž ์‚ฌ์ด์— ์Šค

programmers.co.kr

 

์ฝ”๋“œ

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

using namespace std;

map<int,int>m;
vector<pair<int,double>>fault;

bool cmp(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;

    for(int i=0; i<stages.size(); i++){
        m[stages[i]]++;
    }

    for(int i=1; i<=N; i++){
        int total_peo=0;
        for(auto k=m.begin(); k!=m.end(); k++){
            if(k->first>=i){
                total_peo+=k->second;
            }
        }

        if(total_peo==0) fault.push_back({i,0});
        else{
         double fault_p=(double)m[i]/(double)total_peo;
         fault.push_back({i,fault_p});
        }
    }
    sort(fault.begin(),fault.end(),cmp);

    for(int i=0; i<fault.size(); i++){
        answer.push_back(fault[i].first);
    }

    return answer;
}

 

๋Œ“๊ธ€