k번째 수

이윤설·2024년 3월 15일
0

작성 코드(수정 전)

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int commandsLength = commands.length;
        int[] result = new int[commandsLength];

        for (int i = 0; i < commandsLength; i++) {
            int[] command = commands[i]; // {2,5,3}
            int start = command[0];
            int end = command[1];
            int extract = command[2];
            int[] tempArray = new int[end - start];

            for (int j = start; j < end; j++) {
                tempArray[j] += array[start];
            }

//            Arrays.sort(tempArray);


            int singleNumber = tempArray[extract];
            result[i] += singleNumber;
        }

        for (int i = 0; i < result.length; i++) {
            System.out.print(result);
        }

        return result;
    }
}

작성 코드(수정 후)

import java.util.Arrays;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int commandsLength = commands.length;
        int[] result = new int[commandsLength];

        for (int i = 0; i < commandsLength; i++) {
            int[] command = commands[i]; // {2,5,3}
            int start = command[0] - 1; // 인덱스 조정
            int end = command[1];
            int extract = command[2] - 1; // 인덱스 조정
            int[] tempArray = new int[end - start]; // 크기 조정

            for (int j = start; j < end; j++) {
                tempArray[j - start] = array[j]; // 올바른 복사 로직
            }

            selectionSort(tempArray); // 정렬

            result[i] = tempArray[extract]; // 올바른 추출 로직
        }

        for (int i = 0; i < result.length; i++) {
            System.out.print(Arrays.toString(result)); // 올바른 출력 방법
        }

        return result;
    }

    public static void selectionSort(int[] arr) {
        int n = arr.length;

        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }
    }
}

모범답안

import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int a = 0;
        for(int[] info : commands){
            int i = info[0];
            int j = info[1];
            int k = info[2];

            int[] buf = Arrays.copyOfRange(array,i-1,j);
            Arrays.sort(buf);
            answer[a] = buf[k-1];
            a++;
        }

        return answer;
    }
}

배운 점

  • 실제 인덱스는 0부터 시작하지만, 문제에서는 인덱스를 +1한 값으로 설정하였기 때문에 command[0] -1 을 한 것이다.
for (int j = start; j < end; j++) {
    tempArray[j] += array[start]; // 내가 썼던 복사 로직
}
            
for (int j = start; j < end; j++) {
    tempArray[j - start] = array[j]; // 올바른 복사 로직
}
  • 내가 쓴 코드는 [1,1,1]이 된다. 왜냐하면 array[start] = 1이고, 이를 3번 반복하면 [1,1,1]이 되기 때문이다.

  • 올바른 복사 로직은 += 가 아닌 =을 사용하는 것이다.

  • Arrays.copyOfRange()는 첫 번째 인자로 시작 인덱스를, 두 번째 인자로는 종료 인덱스를 받지만, 종료 인덱스는 범위에 포함되지 않는다. 예를 들어, Arrays.copyOfRange(array, 0, 4); 라고 호출하면, array 배열의 0번째 인덱스부터 3번째 인덱스까지의 요소, 즉 총 4개의 요소를 복사하여 새 배열을 생성한다. 4번째 인덱스의 요소는 포함되지 않는다. 그렇기 때문에 copyofRange(array, i-1, j)라고 쓰는 것이다. 왜냐하면 이미 j-1이 적용되어 있기 때문이다.

profile
화려한 외면이 아닌 단단한 내면

0개의 댓글