본문 바로가기
문제풀이/프로그래머스

[프로그래머스] Level 1 – K번째수

by chan10 2021. 1. 27.

문제 설명 

배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.

예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면

1.     array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.

2.     1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.

3.     2에서 나온 배열의 3번째 숫자는 5입니다.

배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.

 

제한사항

·         array의 길이는 1 이상 100 이하입니다.

·         array의 각 원소는 1 이상 100 이하입니다.

·         commands의 길이는 1 이상 50 이하입니다.

·         commands의 각 원소는 길이가 3입니다.

 

입출력 예 

array

commands

return

[1, 5, 2, 6, 3, 7, 4]

[[2, 5, 3], [4, 4, 1], [1, 7, 3]]

[5, 6, 3]

 

입출력 예 설명 

[1, 5, 2, 6, 3, 7, 4]를 2번째부터 5번째까지 자른 후 정렬합니다. [2, 3, 5, 6]의 세 번째 숫자는 5입니다.
[1, 5, 2, 6, 3, 7, 4]를 4번째부터 4번째까지 자른 후 정렬합니다. [6]의 첫 번째 숫자는 6입니다.
[1, 5, 2, 6, 3, 7, 4]를 1번째부터 7번째까지 자릅니다. [1, 2, 3, 4, 5, 6, 7]의 세 번째 숫자는 3입니다.

 


- 풀이 과정 -

1.     commands 배열의 지정된 범위([i][0]부터 [i][1])에서 차이를 구해 복사할 배열 temp의 크기로 초기화한다.

2.     배열 array에서 commands 배열의 지정된 범위([i][0]부터 [i][1])에 있는 값들을 복사한다.

3.     복사한 배열을 정렬한다.

4.     복사한 배열에서 commands 배열의 [i][2]위치의 숫자에 맞는 값을 추출하여 ArrayList에 저장한다.

 


- 풀이 코드1 -

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
31
32
33
34
35
36
37
import java.util.ArrayList;
import java.util.Arrays;
 
class Solutions {
    public int[] solutions(int[] array, int[][] commands){
        ArrayList<Integer> list = new ArrayList<Integer>();
 
        for(int i=0;i<commands.length;i++){
            int start = commands[i][0];
            int end = commands[i][1];
            int[] temp = new int[(end-start)+1]; // 1.
            
            int k=0;
            for(int j=start-1;j<end;j++){     // 2.
                temp[k] = array[j];
                k++;
            }
            Arrays.sort(temp); // 3.
            list.add(temp[commands[i][2]-1]); // 4.
        }
        int[] answer = new int[list.size()];
        for(int i=0;i<list.size();i++){
            answer[i] = list.get(i);
        }
        return answer;
    }
}
 
public class kNumber {
    public static void main(String[] args) {
        Solutions returns = new Solutions();
        int[] array = {1,5,2,6,3,7,4};
        int[][] commands = {{2,5,3},{4,4,1},{1,7,3}};
 
        System.out.println(Arrays.toString(returns.solutions(array, commands)));
    }
}
 
 

 

 

- 풀이 코드2 -

풀이코드 1에서 했던 배열의 일부를 추출하여 복사하는 코드를 Arrays.copyOfRange()를 사용한 코드

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
import java.util.Arrays;
 
class Solutions {
    public int[] solutions(int[] array, int[][] commands){
        int[] answer = new int[commands.length];
 
        for(int i=0;i<commands.length;i++){
            // 배열의 일부를 선택하여 복사
            int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
            Arrays.sort(temp);
            answer[i] = temp[commands[i][2]-1];
        }
        return answer;
    }
}
 
 
public class kNumber {
    public static void main(String[] args) {
        Solutions returns = new Solutions();
        int[] array = {1,5,2,6,3,7,4};
        int[][] commands = {{2,5,3},{4,4,1},{1,7,3}};
 
        System.out.println(Arrays.toString(returns.solutions(array, commands)));
 
    }
}