반응형
문제
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
풀이
인덱스 범위가 초과나지 않도록 주의한다.
코드
import java.util.Arrays;
public class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i=0; i<commands.length; i++) {
int[] arr = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(arr);
answer[i] = arr[commands[i][2] - 1];
}
return answer;
}
}
반응형
'프로그래머스' 카테고리의 다른 글
프로그래머스 - 카카오 프렌즈 컬러링북 (0) | 2022.02.20 |
---|---|
프로그래머스 - 오픈채팅방 (0) | 2022.02.18 |
프로그래머스 - 타겟 넘버 (0) | 2021.08.24 |
프로그래머스 - 문자열 압축 (0) | 2021.08.22 |
프로그래머스 - 키패드 누르기 (0) | 2021.08.21 |