개발 공부

(프로그래머스)(자바) K번째수 본문

알고리즘

(프로그래머스)(자바) K번째수

아이셩짱셩 2021. 7. 7. 16:22

코딩테스트연습-정렬-k번째수

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

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[] comArr = commands[i];
	            int[] newArr = new int[comArr[1]-comArr[0]+1];
	            
	            int index = 0;
	            for(int j = 0; j < newArr.length; j++ ){
		            //System.out.println(newArr.length+"/"+comArr[0]+"/"+index);
	                newArr[j] = array[comArr[0]-1+j];
	                index++;
	            }
	            
	            Arrays.sort(newArr);
	            //System.out.println(newArr[0]);
	            //System.out.println(newArr.length+"//"+ (comArr[2]-1));
	            answer[i] = newArr[comArr[2]-1];
	        }
	        
	        return answer;
    }
}
Comments