Algorithm

[프로그래머스] 명예의 전당(1)

moguogu 2023. 2. 22. 22:57

문제설명

알고리즘

1. 점수 값을 조회하면서 다른 벡터에 값을 넣는다

2. 내림차순으로 정렬한다

3. K번째가 되기전까지는 가장 큰 인덱스 값이 최하 값이고, K번째까지 벡터에 들어왔을 경우 K-1번째가 최하값이므로 저장한다

코드

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

vector<int> solution(int k, vector<int> score) {
    vector<int> answer;
    vector<int> temp;
    
    for(int i=0; i< score.size(); i++){
        int current = score[i];

        temp.push_back(current);
        sort(temp.rbegin(), temp.rend()); // 내림차순 정렬
                 
        if(i<k) // K이전의 경우 맨 끝 값이 최하
            answer.push_back(temp[temp.size()-1]); 
        else //K개 채운 경우 K-1이 최하값
            answer.push_back(temp[k-1]);
    }
    
    return answer;
}

 

'Algorithm' 카테고리의 다른 글

[프로그래머스] 크기가 작은 부분 문자열  (0) 2023.02.22
[프로그래머스] 귤 고르기  (0) 2023.02.22
[프로그래머스] 과일장수  (0) 2022.11.14
[백준 7569] 토마토  (0) 2022.07.11
[백준 5525] IOIOI  (0) 2022.07.11