본문 바로가기
알고리즘/프로그래머스

[프로그래머스] (정렬) K번째 수

by 오이가지아빠 2021. 3. 22.

핵심은 Arrays.copyOfRange 를 사용하는 것.

제출후에 다시보니 쓸데 없이 List를 사용한 것 같음. 괜히 List를 Array로 변환하는 작업만 추가됨.

 

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];

        List<Integer> ans = new ArrayList<Integer>();

        for (int[] command : commands) {
            int[] tmp = Arrays.copyOfRange(array, command[0]-1, command[1]);
            Arrays.sort(tmp);
            int t = tmp[command[2]-1];
            ans.add(t);
        }

        for (int i = 0; i < ans.size(); i++) {
            answer[i] = ans.get(i);
        }
        return answer;
    }
}

 

출처: 프로그래머스 코딩 테스트 연습, 
https://programmers.co.kr/learn/challenges
반응형

댓글