Posts [프로그래머스] K번째수 / C++
Post
Cancel

[프로그래머스] K번째수 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


  1. 풀이 1,2 모두 tmp 벡터에 array의 i~j번째까지 자른 값을 담아줍니다.

    풀이 2의 경우 벡터의 복사 생성자를 이용하여 슬라이싱

  2. sort 함수로 정렬합니다.

  3. tmp 벡터의 k번째 수를 answer 벡터에 담아줍니다.


소스 코드



풀이 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <vector>

using namespace std;

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


풀이 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <vector>

using namespace std;

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



This post is licensed under CC BY 4.0 by the author.

[프로그래머스] 체육복 / C++

[Apache] 아파치 기동 오류 - so 모듈 관련

Comments powered by Disqus.