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 <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
	vector<int> answer;
	vector<int> temp;
	int first = 0;
	int end = 0;
	int pick = 0;
	int temp2 = 0;

	for (int i = 0; i < commands.size(); i++)
	{
		first = commands[i][0];
		end = commands[i][1];
		pick = commands[i][2];
		for (int j = first - 1; j < end; j++)
		{
			temp.push_back(array[j]);
		}
		sort(temp.begin(), temp.end());
		answer.push_back(temp[pick - 1]);
		temp.clear();
		vector<int>().swap(temp);
	}

	return answer;
}

int main() {
	vector<int> absolutes = { 1, 5, 2, 6, 3, 7, 4 };
	vector<vector<int>> commands = { {2, 5, 3}, { 4, 4, 1}, { 1, 7, 3 } };

	solution(absolutes, commands);
}

'알고리즘 > Coding Test' 카테고리의 다른 글

추억점수 C++  (0) 2023.04.14
달리기 경주 C++  (0) 2023.04.14
소수 만들기 C++  (0) 2022.05.19
음양 더하기 C++  (0) 2022.05.19
없는 숫자 더하기 C++  (0) 2022.05.19

+ Recent posts