https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하세요.
풀이 방법
간단한 구현문제 입니다.
시키는대로 배열을 자르고 정렬해서 원하는 위치의 숫자를 구하면 됩니다.
풀이 코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for(int i=0; i<commands.size(); i++){
vector<int>tmp;
for(int j=commands[i][0]-1; j<commands[i][1]; j++){
tmp.push_back(array[j]);
}
sort(tmp.begin(), tmp.end());
answer.push_back(tmp[commands[i][2]-1]);
}
return answer;
}
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스] Lv.1 키패드 누르기 c++ (0) | 2025.01.17 |
---|---|
[프로그래머스] Lv.1 신규 아이디 추천 c++ (0) | 2025.01.17 |
[프로그래머스] Lv.2 타겟 넘버 c++ (0) | 2025.01.16 |
[프로그래머스] Lv.2 전화번호 목록 c++ (0) | 2025.01.15 |
[프로그래머스] Lv.1 완주하지 못한 선수 c++ (0) | 2025.01.14 |