본문 바로가기
알고리즘/Programmers

[Programmers] k번째 수

by 소꿍 2020. 10. 3.

programmers.co.kr/learn/courses/30/lessons/42748

 

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

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

programmers.co.kr

 

Java

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
	public int[] solution(int[] array, int[][] commands) {
		int[] answer = new int[commands.length];
		List<Integer> tempList = new ArrayList<>();
        for(int[] arr : commands) {
        	int i = arr[0];
        	int j = arr[1];
        	int k = arr[2];
        	int[] tempArray = Arrays.copyOfRange(array, i-1, j);
        	Arrays.sort(tempArray);
        	int num = tempArray[k-1];
        	tempList.add(num);
        }
        for(int i = 0; i < answer.length; ++i) {
        	answer[i] = tempList.get(i);
        }
        return answer;
 
	}
}

댓글