-
반응형
programmers.co.kr/learn/courses/30/lessons/42748
코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
문제풀이)
이건 벡터 사용법을 익히는 문제같다. 해당 commands에 맞는 수행을 한 뒤, sort함수를 이용하여 정렬하고, 정렬한 배열에서 k번째 수를 answer에 push_back해주면 된다.
#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> temp; int st=commands[i][0]; int end=commands[i][1]; int k=commands[i][2]; for(int j=st-1;j<end;j++){ temp.push_back(array[j]); } sort(temp.begin(),temp.end()); answer.push_back(temp[k-1]); } return answer; }
반응형'문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 가운데 글자 가져오기 (0) 2020.11.24 [프로그래머스] 2016년 (0) 2020.11.24 [프로그래머스] 체육복 (0) 2020.11.24 [프로그래머스] 모의고사 (0) 2020.11.23 [프로그래머스] 완주하지 못한 선수 (0) 2020.11.23 댓글