cosmohoo 2021. 11. 25. 20:31
반응형

문제설명

 

 

 

입출력 예 설명

[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입니다.

 

 

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    int comLimit = commands.size();
    for(int i=0; i<comLimit; i++)
    {
        vector<int> tmp;
        for(int j=commands[i][0]; j<=commands[i][1]; j++)
        {
            tmp.push_back(array[j-1]);
        }
        sort(tmp.begin(),tmp.end());
        int pushIndex = commands[i][2]-1;
        answer.push_back(tmp[pushIndex]);
        tmp.clear();
    }
    return answer;
}

 

 

=> 각 배열에 첫 번째, 두 번째, 세 번째 원소를 사용해 원하는 만큼 자르고 K번째에 있는 수를 찾는 문제였습니다. 

=> 컴퓨터에서 배열의 첫번째는 '0'번째이지만, 여기서는 '1'번째라고 부르기 때문에 해당사항을 염두에 둬야 합니다. 

반응형