[Programmers] K번째 수 - 정렬

동민·2021년 3월 10일
0
import java.util.Arrays;

// Arrays.copyOfRange 사용하는 문제 ; copyOfRange : 전달받은 배열의 특정 범위에 해당하는 요소만을 새로운 배열로 복사하여 반환합니다.
// K번째 수 - 정렬
public class NumberK {

	public int[] solution(int[] array, int[][] commands) {
		int[] answer = new int[commands.length];
		int[] temp;
		int s, e, n;

		for (int i = 0; i < commands.length; i++) {
			
			s = commands[i][0];
			e = commands[i][1];
			n = commands[i][2];

			temp = new int[e - s + 1];

			temp = Arrays.copyOfRange(array, s - 1, e); // 전달받은 배열의 특정 범위에 해당하는 요소만을 새로운 배열로 복사하여 반환.

			Arrays.sort(temp);

			answer[i] = temp[n - 1];
		}
		return answer;
	}

	public static void main(String[] args) {

		NumberK n = new NumberK();
		int[] arr = { 1, 5, 2, 6, 3, 7, 4 };
		int[][] com = { { 2, 5, 3 }, { 4, 4, 1 }, { 1, 7, 3 } };

		for (int i = 0; i < n.solution(arr, com).length; i++) {
			System.out.print(n.solution(arr, com)[i] + " ");
		}
		n.solution(arr, com);

	}
}
  • Arrays.copyOfRange 사용하는 문제 ; copyOfRange : 전달받은 배열의 특정 범위에 해당하는 요소만을 새로운 배열로 복사하여 반환
arr1 = Arrays.copyOfRange(arr2, int start_index, int end_index);
profile
BE Developer

0개의 댓글