코드입니다
import java.util.Scanner;
public class Main {
static int[] temp;
static int count = 0;
static int K;
static int result = -1;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int[] arr = new int[num];
K = scan.nextInt();
temp = new int[num];
for(int i = 0 ; i < num ; i++){
arr[i] = scan.nextInt();
}
merge_sort(arr, 0, arr.length - 1);
System.out.print(result);
}
static void merge_sort(int A[], int low, int high){
if(low < high){
int mid = (low + high) / 2;
merge_sort(A, low, mid);
merge_sort(A, mid + 1, high);
merge(A, low, mid, high);
}
}
static void merge(int A[], int low, int mid, int high){
int i = low;
int j = mid + 1;
int t = 0;
while(i <= mid && j <= high){
if(A[i] <= A[j]){
temp[t++] = A[i++];
}else{
temp[t++] = A[j++];
}
}
while(i <= mid){
temp[t++] = A[i++];
}
while(j <= high){
temp[t++] = A[j++];
}
t = 0;
i = low;
while(i <= high){
count++;
if(count==K){
result = temp[t];
break;
}
A[i++] = temp[t++];
}
}
}
'백준 문제풀기 > JAVA' 카테고리의 다른 글
[백준 1822 JAVA 자바] 차집합 (0) | 2023.08.31 |
---|---|
[백준 1731 JAVA 자바] 추론 (0) | 2023.08.31 |
[백준 19532 JAVA 자바] 수학은 비대면강의입니다 (0) | 2023.08.30 |
[백준 18258 JAVA 자바] 큐 2 (0) | 2023.08.29 |
[백준 18108 JAVA 자바] 1998년생인 내가 태국에서는 2541년생?! (0) | 2023.08.29 |