[프로그래머스/C++]Lv.1 - 명예의 전당

YH J·2023년 6월 1일
0

프로그래머스

목록 보기
106/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/138477

내 풀이

우선순위 큐 priority queue를 사용하였다. 내림차순으로 정렬하도록 설정한 뒤 score의 원소들을 추가해가면서 자동으로 정렬되므로 top()이 가장 낮은 점수이다.
k + 1 일차 부터 pop()으로 가장 낮은 점수를 뺀다.

내 코드

#include <string>
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

vector<int> solution(int k, vector<int> score) {
    vector<int> answer;
    
    priority_queue<int, vector<int>, greater<int>> q;
    
    for(int i = 0; i < score.size(); i++)
    {
        q.push(score[i]);
        if(i + 1 > k)
            q.pop();
        answer.push_back(q.top());
    }
    return answer;
}

다른 사람의 풀이

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

using namespace std;

vector<int> solution(int k, vector<int> score) {
    vector<int> answer, tmp;

    for(auto s : score){
        tmp.push_back(s);
        sort(tmp.begin(), tmp.end(), greater<int>());
        if(tmp.size() >= k) tmp.erase(tmp.begin() + k, tmp.end());
        answer.push_back(tmp.back());
    }

    return answer;

}

다른 사람의 풀이 해석

score을 넣을 때 마다 sort를 하였다.

profile
게임 개발자 지망생

0개의 댓글