알고리즘/프로그래머스 Level1

[프로그래머스,Java] Level1: K번째수

류창 2021. 8. 6. 16:36
반응형

문제분석: 

 

할게없다 그냥 시키는대로 하면된다. 배열을 자르고, 정렬하고, 뽑으면댄다

 

1
2
3
4
5
6
7
8
9
10
11
12
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++) {
            int[] temp = Arrays.copyOfRange(array, commands[i][0]-1,commands[i][1]);
            Arrays.sort(temp);
            answer[i]=temp[commands[i][2]-1];
        }
        return answer;
    }
}
cs

 

문제풀이:

자바 메소드(API)를 어느정도 알고있다면 쉽다.

 

1.자르기 :Arrays.copyOfRange(array,자를 첫번째위치, 자를 두번째위치) ->잘랐다면 자른배열 반환

2.정렬 : Arrays.sort(temp); ->정렬

3.뽑기 

반응형