1. 나의 풀이
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int [] answer = new int[commands.length];
ArrayList <Integer> imsi = new ArrayList<Integer>();
for(int i = 0; i < commands.length; i = i + 1) {
for(int j = commands[i][0]; j <= commands[i][1]; j = j + 1) {
imsi.add(array[j-1]);
}
imsi.sort(Comparator.naturalOrder());
answer[i] = imsi.get(commands[i][2]-1);
imsi.clear();
}
return answer;
}
}
2. 다른 사람 풀이
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;
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 - 연습문제 - 2016년 (0) | 2020.09.17 |
---|---|
프로그래머스 코딩테스트 연습 - 탐욕법(Greedy) - 체육복 (0) | 2020.09.17 |
프로그래머스 코딩테스트 연습 - 완전탐색 - 모의고사 (0) | 2020.09.16 |
프로그래머스 코딩테스트 연습 - 월간 코드 챌린지 시즌1 - 두 개 뽑아서 더하기 (0) | 2020.09.16 |
프로그래머스 코딩테스트 연습 - 가운데 글자 가져오기 (0) | 2020.09.13 |