잡다한 배똥월드

728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

코딩테스트 연습 - K번째수

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

 

 

 

 

 

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for (int i = 0; i < commands.length; i++) {
            int[] arr = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
            Arrays.sort(arr);
            
            answer[i] = arr[commands[i][2]-1];
        }
        return answer;
    }
}

 

테스트 1 통과 (0.35ms, 77.3MB)
테스트 2 통과 (0.46ms, 74.3MB)
테스트 3 통과 (0.33ms, 77.2MB)
테스트 4 통과 (0.45ms, 75.1MB)
테스트 5 통과 (0.33ms, 75.8MB)
테스트 6 통과 (0.32ms, 83MB)
테스트 7 통과 (0.46ms, 75.2MB)

 

 

 

 

범위를 지정해서 배열을 자르는 copyOfRange를 이용해서 자른 후 정렬한 다음에

원하는 위치의 숫자를 가져오면 됨

 

 

 

 

 

728x90

+ Recent posts