-
k번째 수 찾기알고리즘/프로그래머스 2020. 7. 15. 22:58
출처 : 프로그래머스 나의 해답.
import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; for(int q=0; q< commands.length; q++){ int[] arr = commands[q]; int i = arr[0] -1; int j = arr[1] -1; int k = arr[2] -1; List<Integer> list = new ArrayList<Integer>(); for(int l=i; l<=j; l++){ list.add(array[l]); } Collections.sort(list); answer[q] = list.get(k); } return answer; } }
다른 사람 풀이 중 이중배열?의 특징을 잘 이용했고, 처음보는 함수를 사용한걸 보았다.
프로그래머스 문제를 풀때마다 Java 기본 API에 대해 잘 이해하고 알고있는게 중요한것 같다.
for(int i=0; i<commands.length; i++){ int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]); // Arrays.copyOfRange(원본배열, 복사할 시작 인덱스, 복사할 끝 인덱스); Arrays.sort(temp); answer[i] = temp[commands[i][2]-1]; }
'알고리즘 > 프로그래머스' 카테고리의 다른 글
정수 내림차순 (0) 2020.07.26 짝수와 홀수 (0) 2020.07.21 서울에서 김서방 찾기 (0) 2020.07.08 코딩테스트 연습 - 수박수박수박수박수박수? (0) 2020.06.26 스킬 체크 1번 -2번째 (0) 2020.06.24