Problem Solving/프로그래머스
[프로그래머스/C++] K번째수
높은곳에영광
2022. 1. 16. 23:54
#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 size = commands.size();
int te1, te2, te3;
for (int i = 0; i < size; ++i) {
te1 = commands[i][0] -1; te2 = commands[i][1]; te3 = commands[i][2] -1;
answer.assign(array.begin() + te1, array.begin() + te2);
sort(answer.begin(), answer.end());
temp.push_back(answer[te3]);
}
return temp;
}
이 문제는 배열을 commands 배열에 1~2번 째 원소의 숫자만큼 배열을 잘라서 sorting한 후 배열의 3번 째 원소의 값을 return 하는 문제입니다.
te1 과 te2을 통해서 자를 값을 받아 answer라는 새로운 벡터로 그 길이만큼 값을 받은 후, 정렬 하고 te3번 째 원소를 구하였습니다.
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