배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {
private static final int START = 0;
private static final int END = 1;
private static final int PICK = 2;
public int[] solution(int[] array, int[][] commands) {
int[] result = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
List<Integer> cutList = new ArrayList<>();
int[] row = commands[i];
for (int cutIndex = row[START] - 1; cutIndex <= row[END] - 1; cutIndex++) {
cutList.add(array[cutIndex]);
}
Collections.sort(cutList);
result[i] = cutList.get(commands[i][PICK] - 1);
}
return result;
}
}
[프로그래머스] 가운데 글자 가져오기 (0) | 2020.10.17 |
---|---|
[프로그래머스] 2016년 (0) | 2020.10.17 |
[프로그래머스] 체육복 (0) | 2020.10.17 |
[프로그래머스] 모의고사 (0) | 2020.10.17 |
[프로그래머스] 크레인 인형뽑기 게임 (0) | 2020.10.17 |