-
[프로그래머스] 정렬 - k 번째 수알고리즘 공부/문제 풀이 2020. 8. 6. 18:26
각각의 command마다 해당하는 배열을 따로 생성해준뒤, 생성한 배열을 sort하고
n번째 값을 저장하는 방식으로 문제를 풀이했다.
static int[] solution(int[] array, int[][] commands) { // answer의 크기는 command의 길이와 같다 int[] answer =new int[commands.length]; // 각 command에 해당하는 arr를 만들어 준다. for(int i=0;i<commands.length;i++){ int[] arr=new int[commands[i][1]-commands[i][0]+1]; int index=0; // 주어진 array에서 command에 해당하는 값들을 arr에 넣어준다. for(int j=commands[i][0]-1;j<=commands[i][1]-1;j++) { arr[index]=array[j]; index++; } // array 라이브러리를 이용하여 sort해준다 Arrays.sort(arr); // command[i][2]에 해당하는 값을 answer에 넣어준다. // 단 command[i][2]는 index값이 아닌 n번째 숫자를 나타내므로 -1을 해준다 answer[i]=arr[commands[i][2]-1]; } return answer; }
'알고리즘 공부 > 문제 풀이' 카테고리의 다른 글
[프로그래머스] 정렬 - H-index (1) 2020.08.06 [프로그래머스] 정렬 - 가장 큰 수 (1) 2020.08.06 [2019 카카오 겨울인턴] 호텔방 (JAVA) (0) 2020.05.08 [2019 카카오 겨울인턴] 불량사용자 (JAVA) (0) 2020.05.08 [2019 카카오 겨울인턴] 튜플 (JAVA) (0) 2020.05.06