알고리즘/Lv1. 프로그래머스

k번째수

signal시노 2023. 7. 11. 15:40

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

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 [] sub =new int[commands[i][1]-commands[i][0]+1];
            for(int j=0;j<commands[i][1]-commands[i][0]+1;j++){
            
            sub[j]=array[j+commands[i][0]-1];
                
            }
            Arrays.sort(sub);
            answer[i]=sub[commands[i][2]-1];
        }
        
        return answer;
    }
}

배열을 오름차순 정렬하는 Arrays.sort를 안다면 쉽게 풀 수 있을 것 입니다.

'알고리즘 > Lv1. 프로그래머스' 카테고리의 다른 글

푸드 파이트 대회  (0) 2023.07.11
두 개 뽑아서 더하기  (0) 2023.07.11
숫자 문자열과 영단어  (0) 2023.07.11
1차 비밀지도  (0) 2023.07.07
추억 점수  (0) 2023.07.04