어레이를 자주 사용하지 않아, 값을 모두 리스트로 옮긴 다음에 진행하였다.
주의 해야 할 점은, sublist 를 sort 하였을 때 parent에 영향을 미치지 않도록 해야 한다는 것이었다.
다른 사람들의 풀이 방법을 보았을 떄 copyOfRange를 사용하여 문제를 해결한 것이 일반 적이었다.
문제 설명
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
- array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
- 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
- 2에서 나온 배열의 3번째 숫자는 5입니다.
배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
제한사항
- array의 길이는 1 이상 100 이하입니다.
- array의 각 원소는 1 이상 100 이하입니다.
- commands의 길이는 1 이상 50 이하입니다.
- commands의 각 원소는 길이가 3입니다.
입출력 예
arraycommandsreturn
[1, 5, 2, 6, 3, 7, 4] | [[2, 5, 3], [4, 4, 1], [1, 7, 3]] | [5, 6, 3] |
입출력 예 설명
[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class prepareSort1 {
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}};
// [5, 6, 3]
List<Integer> list = new ArrayList<>();
for (Integer number: array) {
list.add(number);
}
int[] answer = new int[3];
int index = 0;
for (int i = 0; i < commands.length; i++) {
int start = commands[i][0];
int end = commands[i][1];
int target = commands[i][2];
List<Integer> subList = new ArrayList<>(list.subList(start - 1, end));
Collections.sort(subList);
answer[index] = subList.get(target - 1);
index ++;
}
System.out.println(Arrays.toString(answer));
}
}
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
int a = 0;
for(int[] info : commands){
int i = info[0];
int j = info[1];
int k = info[2];
int[] buf = Arrays.copyOfRange(array,i-1,j);
Arrays.sort(buf);
answer[a] = buf[k-1];
a++;
}
return answer;
}
}
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++){
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(temp);
answer[i] = temp[commands[i][2]-1];
}
return answer;
}
}