알고리즘/프로그래머스
K번째수
베리영young
2023. 12. 30. 03:51
사용 알고리즘: 정렬
사용 언어: java
import java.util.*;
import java.io.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int c=0; c<commands.length; c++){
int i= commands[c][0]-1; //여기부터
int j= commands[c][1]; //여기까지
int k= commands[c][2]-1;
int[] tmp = new int[j-i];
int idx = 0;
for(int idx2=i; idx2<j; idx2++){
tmp[idx++] = array[idx2];
}
Arrays.sort(tmp);
answer[c] = tmp[k];
}
return answer;
}
}
이슈:
더 깔끔하게 코드를 짤 수 있을 거 같아서 다른 분들의 코드를 찾아보다가
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
copyOfRange라는 메소드 발견!!