코딩테스트/프로그래머스 1단계
프로그래머스 1단계 - K번째수
SICDev
2021. 4. 18. 19:22
반응형
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.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++){
//copyOfRange(배열, 첫번째index(포함), 마지막index(미포함))
//배열은 0부터 시작이니까 첫번째index값에 -1을해준다. 마지막index를 포함하지않으니 자동으로-1이된 셈이다.
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(temp);
//마찬가지로 k번째 있는 수도 배열은 0부터시작이니까 -1을 해줘한다.
answer[i] = temp[commands[i][2]-1];
}
return answer;
}
}
반응형