일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- BOJ
- 구간 합 구하기 4
- 숫자 문자열과 영단어
- Hasing
- 정수 삼각형
- 옵셔널 체이닝 연산자
- 주식 가격
- Git Convention
- 프로그래머스
- colorSyntax
- codeSyntaxHighlight
- 다이내믹 프로그래밍
- 깊이 우선 탐색
- C++
- 5525
- 10162
- 2018 KAKAO BLIND RECRUITMENT
- 이분탐색
- 없는 숫자 더하기
- 소수 체크
- 18111
- 위클리 챌린지
- javascript
- mermaid js
- react
- 브루트포스 알고리즘
- n^2 배열 자르기
- js
- 4796
- 1620
Archives
- Today
- Total
개발하는 kim-hasa
[c++][프로그래머스] K번째수 본문
https://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
sort를 위해 #include <algorithm> 을 추가합니다.
배열에서 특정 index만큼 자른 후 자른 배열의 특정 index를 추출하는 문제입니다.
특정 index 범위만큼을 잘라서 slice 벡터 배열에 넣은 후, 정렬하고 특정 index를 정답 벡터에 넣습니다.
slice 배열을 초기화 하고 반복합니다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
int first; // command 배열의 i번째 숫자
int last; // command 배열의 j번째 숫자
int index; // command 배열 정렬 후 k번째 숫자
vector<int> slice; // 자른 배열
for(int i=0; i < commands.size(); i++)
{
first = commands[i][0];
last = commands[i][1];
index = commands[i][2]; // 숫자 넣기
for(int j=first-1; j<last; j++) // slice배열에 넣기 위해
{
slice.push_back(array[j]);
}
sort(slice.begin(), slice.end()); // 정렬
answer.push_back(slice[index-1]);
slice.clear();
}
return answer;
}
※ 코드가 지저분할 수 있습니다.
'Algorithm > Programmers(c++)' 카테고리의 다른 글
[c++][프로그래머스] 내적 (0) | 2021.07.27 |
---|---|
[c++][프로그래머스] 모의고사 (0) | 2021.07.27 |
[c++][프로그래머스] 완주하지 못한 선수 (0) | 2021.07.26 |
[c++][프로그래머스] 체육복 (0) | 2021.07.26 |
[c++][프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2021.07.26 |