728x90
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
간단히 Collection.sort 를 이용하면 되는 문제다.
import java.util.ArrayList;
import java.util.Collections;
public class NumOfKth {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i = 0; i < commands.length; i ++) {
ArrayList<Integer> list = new ArrayList<>();
for(int j = commands[i][0] - 1; j < commands[i][1]; j ++) {
list.add(array[j]);
}
Collections.sort(list);
answer[i] = list.get(commands[i][2] - 1);
}
return answer;
}
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[] answer = solution(array, commands);
for(int a : answer) System.out.print(a + " ");
}
}728x90
'알고리즘 > 프로그래머스' 카테고리의 다른 글
| 프로그래머스 로또의 최고 순위와 최저 순위 (0) | 2022.04.14 |
|---|---|
| 프로그래머스 신고 결과 받기 자바 풀이 (0) | 2022.04.14 |
| 프로그래머스 N-Queen 자바 풀이 (0) | 2022.04.10 |
| 프로그래머스 단어변환 자바 풀이 (0) | 2022.03.09 |
| 프로그래머스 여행경로 자바 풀이 (0) | 2022.03.08 |