본문 바로가기
알고리즘 & 자료구조/프로그래머스

K번째수

by 신재권 2022. 10. 29.
package programmers;

import java.util.Arrays;

public class K번째수 {

   public static int[] solution(int[] array, int[][] commands) {
      int[] answer = new int[commands.length];
      for (int i = 0; i < commands.length; i++) {
         int[] sliceArray = Arrays.copyOfRange(array, commands[i][0] - 1, commands[i][1]);
         Arrays.sort(sliceArray);
         answer[i] = sliceArray[commands[i][2] - 1];
      }

      return answer;
   }

   public static void main(String[] args) {
      System.out.println(
         Arrays.toString(solution(new int[] {1, 5, 2, 6, 3, 7, 4}, new int[][] {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}})));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

삼총사  (0) 2022.10.30
두 개 뽑아서 더하기  (0) 2022.10.30
문자열 내 마음대로 정렬  (0) 2022.10.29
최소직사각형  (0) 2022.10.23
비밀지도  (0) 2022.10.22