문제 링크입니다: 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
간단한 정렬 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 코딩테스트 고득점 Kit] H-Index (0) | 2021.09.21 |
---|---|
[Programmers 코딩테스트 고득점 Kit] 가장 큰 수 (0) | 2021.09.21 |
[Programmers 코딩테스트 고득점 Kit] 순위 (0) | 2021.09.21 |
[Programmers 코딩테스트 고득점 Kit] 방의 개수 (0) | 2021.09.21 |
[Programmers 코딩테스트 고득점 Kit] 가장 먼 노드 (0) | 2021.09.21 |