알고리즘/programmers

[Programmers 코딩테스트 고득점 Kit] K번째수

꾸준함. 2021. 9. 21. 18:26

문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/42748

 

코딩테스트 연습 - K번째수

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

간단한 정렬 문제였습니다.

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for (int c = 0; c < commands.size(); c++)
{
int i = commands[c][0];
int j = commands[c][1];
int k = commands[c][2];
vector<int> tempArray = array;
sort(tempArray.begin() + i - 1, tempArray.begin() + j);
answer.push_back(tempArray[i + k - 2]);
}
return answer;
}
int main(void)
{
vector<int> array = { 1, 5, 2, 6, 3, 7, 4 };
vector<vector<int>> commands = {
{2, 5, 3},
{4, 4, 1},
{1, 7, 3}
};
vector<int> answers = solution(array, commands);
for (int answer : answers)
{
cout << answer << " ";
}
cout << "\n";
return 0;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2017

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형