Home 프로그래머스(K번째수)
Post
Cancel

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

첫 시도

  • 딱히 알고리즘 알아야하는 문제가 아님
  • 범위를 정하고 범위 내 값들을 정렬해 답을 찾아내는 문제

해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.*;

class Solution {

    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];

        int T = commands.length;
        for (int t = 0; t < T; t++) {
            int i = commands[t][0]-1;
            int j = commands[t][1]-1;
            int k = commands[t][2]-1;

            int[] arr = new int[j - i + 1];

            for (int x = i; x <= j; x++) {
                arr[x - i] = array[x];
            }
            Arrays.sort(arr);
            answer[t] = arr[k];

        }
        return answer;
    }
}


참고

  • 직접구현
This post is licensed under CC BY 4.0 by the author.