[프로그래머스 문제풀이] 15. K번째수

WIGWAG·2023년 1월 3일
0

프로그래머스

목록 보기
15/32

이 문제를 요약하면 컨테이너를 자르고 정렬하고 원소를 고르는 문제이다.

임시벡터를 하나 생성해서 이터레이터 범위에 있는 원소들을 복사한다.

vector<int> tmp(array.begin() + command[0] - 1, array.begin() + command[1]);

#include <string>
#include <vector>
#include <algorithm>

#include <iostream>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;

    for (auto command : commands)
    {
		vector<int> tmp(array.begin() + command[0] - 1, array.begin() + command[1]);
        sort(tmp.begin(), tmp.end());
        answer.push_back(tmp[command[2] - 1]);
    }

    return answer;
}

int main()
{
    for (auto d : solution({ 1, 5, 2, 6, 3, 7, 4 }, {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}} ))
    {
        cout << d << ' ';
    }
}

실행결과

5 6 3


K번째수 문제 링크

profile
윅왁의 프로그래밍 개발노트

0개의 댓글