Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- qorwns
- 별 찍기 10
- 원판 돌리기
- dfs
- 스택의 특징
- AVL 시간 복잡도
- 해시 구현
- 백준 17822
- 구현
- 백준 2447
- 게리멘더링2
- 백준 1158
- 백준 17779
- 5397
- 백준 1406
- C/C++ 구현
- heap
- 풀이
- 자료구조
- c#
- 조세퍼스 순열
- 백준
- 해시구현
- 버킷 정렬
- Stack 이란
- 시간 복잡도
- 백준 17471
- 백준 5397
- 1764
- ㅣ풀이
Archives
- Today
- Total
홍시홍의 프로그래밍
[프로그래머스] k번째 수 본문
분류 : 정렬
요구사항
index (x,y)의 사이에 있는 수 중 k번째 수 구하기
풀이
(x,y)번째 중 k번째 수는
오름 차순 정렬 한 후, 원래 인덱스에서의 위치가 (x,y) 사이라면 cnt를 증가 시킨다.
cnt가 k일경우, 정답 출력
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct go {
int num;
int index;
};
bool com(go a, go b) {
if (a.num < b.num) return true;
return false;
}
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
vector<go> myarray;
for (int i = 0; i < array.size(); i++) {
myarray.push_back({ array[i],i+1 });
}
sort(myarray.begin(), myarray.end(), com);
for (int i = 0; i < myarray.size(); i++) {
cout << myarray[i].num << " ";
}
cout << endl;
for (int i = 0; i < commands.size(); i++) {
int x = commands[i][0];
int y = commands[i][1];
int z = commands[i][2];
int cnt = 0;
for (int j = 0; j < myarray.size(); j++) {
int index = myarray[j].index;
int num = myarray[j].num;
cout << "x" << x << "Y" << y << "index" << index << endl;
if (index >= x && index <= y) {
cnt++;
}
if (cnt == z) {
answer.push_back(num);
break;
}
}
}
return answer;
}
'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 섬 연결하기 (0) | 2020.04.20 |
---|---|
[프로그래머스] 가장 큰 수 (0) | 2020.04.18 |
[프로그래머스] 단어 변화 (0) | 2020.04.12 |
[프로그래머스] 네트워크 (0) | 2020.04.10 |
[프로그래머스] 타겟 넘버 (0) | 2020.04.10 |
Comments