IT 개발자_S

[프로그래머스] k번째수 본문

IT/알고리즘_JAVA

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

Soso12 2020. 6. 1. 21:38
반응형

1. 배열이 주어지고

2. 배열의 구간을 정해서 정렬후 특정위치의 값을 answer

3. Arrays.sort 배열정리 사용

import java.util.*;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = {};

        answer = new int [commands.length];

        for (int i =0; i < commands.length ; i++){            
            int start = commands[i][0];
            int end = commands[i][1];
            int point = commands[i][2];
            int length = end-start +1;
            int [] arr = new int[length];

            start -=1;
            for( int j =0 ; j <length ; j++){
                arr[j] = array[start++];
                System.out.println(arr[j] + "  " + array[j]);
            }
            Arrays.sort(arr);

            answer[i] = arr[point-1];

        }


        return answer;
    }
}

 

다른사람의 코드 

copyoRange 를 통해 for문을 안돌리고 단축  심플하게 가능

   int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
반응형

'IT > 알고리즘_JAVA' 카테고리의 다른 글

[프로그래머스] 소수찾기  (2) 2022.03.23
[프로그래머스] 가장큰수  (0) 2020.06.01
[프로그래머스] 탑  (0) 2020.05.31
[프로그래머스] 완주하지 못한 선수  (0) 2020.05.27
[코딜리티]FrogRiverOne  (0) 2020.05.11
Comments