Coding Test/프로그래머스

[프로그래머스] k번째수 - 자바

lsh2613 2023. 8. 10. 15:24

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

 

프로그래머스

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

programmers.co.kr

문제 풀이

단순 정렬을 통한 인덱싱 문제여서 쉽지만 스트림을 연습 중이여서 스트림을 통해 더욱 간결한 코드로 구현

코드

import java.util.stream.*;
import java.util.*;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        ArrayList<Integer> answer = new ArrayList();
        for(int i=0; i<commands.length; i++){
            
            int kNum =IntStream.range(commands[i][0]-1, commands[i][1])
                    .map(n -> array[n])
                    .sorted()
                    .toArray()[commands[i][2]-1];
            answer.add(kNum);
        }
            
        return answer.stream().mapToInt(Integer::intValue).toArray();

    }
}