Hodustory/프로그래밍&DB

프로그래머스 : K번째 수(JAVA)

호두밥 2021. 7. 22. 17:54

https://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

import java.util.Arrays;

class Solution {
    
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        int a = 0;
        
        for(int[] c : commands) {
        	
        	int[] result = new int[c[1]-c[0]+1]; //자른 문자열
        	int r = 0;
        	
            //1. 문자열 자르기
        	for(int i=c[0]-1; i < c[1]; i++) {
        		result[r] = array[i];
        		r++;
        	}
        	//2. 문자열 정렬
        	Arrays.sort(result);
            //3. k번째 수 찾기
        	answer[a] = result[c[2]-1];
        	a++;
        }
        
        
        return answer;
    
    }
}
반응형