IT STUDY LOG

[JAVA] 프로그래머스: K번째수 본문

computer science/coding test

[JAVA] 프로그래머스: K번째수

roheerumi 2023. 5. 3. 09:32

# 문제 내용

프로그래머스: K번째수

 

프로그래머스

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

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++) {
            // commands[i][0] = 시작
            // commands[i][1] = 끝
            // commands[i][2] = 출력 숫자
   
            int[] tmp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
            Arrays.sort(tmp);
            answer[i] = tmp[commands[i][2]-1];
        }
        return answer;
    }
}

 

Comments