알고리즘/문제풀이
[프로그래머스] 42748. K번째
BSHwan
2025. 7. 24. 23:55
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for (int j = commands[i][0] - 1; j <= commands[i][1] - 1; j++) {
arrayList.add(array[j]);
}
Collections.sort(arrayList);
answer[i] = arrayList.get(commands[i][2] - 1);
// System.out.println(arrayList.toString());
// System.out.println(answer[i]);
}
// System.out.println(answer.toString());
return answer;
}
}
반응형