[프로그래머스/Java] K번째수

괜찮아요?·2023년 4월 5일
0

programmers

목록 보기
19/23

링크

코딩테스트 연습 > 정렬 > K번째수

풀이방법

  1. answer 배열의 길이는 command의 length
  2. copyOfRange를 사용해서 정해진 번째로 배열을 잘라서 splitedArr에 저장
  3. 해당하는 인덱스의 값을 배열에 삽입

코드

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[] splitedArr = Arrays.copyOfRange(array,commands[i][0]-1,commands[i][1]);
            Arrays.sort(splitedArr);
            int index = commands[i][2]-1;
            answer[i] = splitedArr[index];
        }
        return answer;
    }
}```
profile
할 수 있어요

0개의 댓글