[Programmers] 정렬 : k번째수
- 💾 알고리즘/프로그래머스
- 2021. 6. 4.
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
package com.hyosung.algo;
import java.util.Scanner;
public class pro_sort02 {
static int[] buff;
static void swap(int[]a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
static void quickSort(int[] a, int left, int right) {
int pl = left;
int pr = right;
int pivot = a[(pl+pr)/2];
do{
while(a[pl] < pivot) pl++;
while(a[pr] > pivot) pr--;
if(pl <= pr) {
swap(a, pl++, pr--);
}
}while(pl <= pr);
if(left < pr) quickSort(a, left, pr);
if(pl < right) quickSort(a, pl, right);
}
public static void main(String[] args) {
int[] array = {1,5,2,6,3,7,4};
int[][] commands = {
{2,5,3},
{4,4,1},
{1,7,3}
};
int[] aa = Solution(array, commands);
viewArray(aa);
}
static int[] Solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i < commands.length; i++) {
int start = commands[i][0];
int end = commands[i][1];
int k = commands[i][2];
buff = new int[end-start+1];
int cnt = 0;
for(int b=start-1; b < end; b++ ) {
//System.out.println(array[i]);
buff[cnt++] = array[b];
}
quickSort(buff, 0, buff.length-1);
viewArray(buff);
answer[i] = buff[k-1];
//System.out.println("result ; " + buff[k-1]);
}
return answer;
}
static void viewArray(int[] a) {
for(int i=0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
'💾 알고리즘 > 프로그래머스' 카테고리의 다른 글
[Programmers] : 해시 > 전화번호 목록 > JAVA (0) | 2021.08.05 |
---|---|
[Programmers] : 해시 > 완주하지 못한 선수 (0) | 2021.07.30 |
[Programmers] : 스택/큐 :: 주식가격 (0) | 2021.07.26 |
[Programmers] : 스택/큐 :: 프린터 (0) | 2021.07.23 |
[Programmers] 네트워크 DFS 풀이 (Java) (0) | 2020.07.21 |