프로그래밍응용/오답노트
벡터 복사 및 sort 이용
photoner
2021. 2. 13. 22:09
728x90
반응형
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
출처
[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> part_vector;
vector<int> answer;
int i=0, j=0, k=0;
// commands로부터 조건들을 꺼낸다.
for(auto it=commands.begin(); it!=commands.end(); it++)
{
i = (*it)[0]-1, j = (*it)[1]-1, k = (*it)[2]-1; // 1번째는 0번째이므로 -1 처리.
part_vector.resize(j-i+1); // j-i+1 은 부분 벡터의 크기
copy(array.begin()+i, array.begin()+j+1, part_vector.begin()); // 부분 벡터를 복사
sort(part_vector.begin(), part_vector.end()); // 정렬(퀵)
answer.push_back(part_vector[k]); // 정렬된 부분 벡터에서 k번째를 구함.
}
return answer; // 간단
}
|
cs |
728x90
반응형