[프로그래머스] K번째수 - java

ImOk·2021년 11월 30일
0
post-thumbnail

K번째수


💡 문제 설명

배열 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입니다.

🔑 입출력 예

arraycommandsreturn
[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 - java

  • Arrays.sort() : java.util.Arrays; 배열[]을 오름차순 정렬
import java.util.*;
class Solution {
			       // {1,5,2,6,3,7,4} / {{2,5,3},{4,4,1},{1,7,3}}
	public static int[] solution(int[] array, int[][] commands) { 
		int[] answer = new int[commands.length]; // 3
		int n = 0;
		
		for (int i = 0; i < commands.length; i++) {
			int k = 0;
                                    // new int[4] // new int[1] // new int[7]
			int[] list = new int[commands[i][1] - commands[i][0] + 1]; 
                                     // j = 2 ~ 5 / j = 4 ~ 4 / j= 1 ~ 7
			for (int j = commands[i][0]; j <= commands[i][1]; j++) { 
				list[k++] = array[j - 1]; // {5,2,6,3} / {6} / {1,5,2,6,3,7,4}
			}
			Arrays.sort(list); // 오름차순 정렬 {2,3,5,6} / {6} / {1,2,3,4,5,6,7}
			answer[n++] = list[commands[i][2] - 1]; // {5,6,3}
		}
		return answer;
	}
}

💻 작성 코드 2 - java

  • Collections.sort() : java.util.Collections; ArrayList 등을 오름차순 정렬
import java.util.*;
class Solution {
	public int[] solution(int[] array, int[][] commands) {
		int[] answer = new int[commands.length];
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (int i = 0; i < commands.length; i++) {
			for (int j = commands[i][0]; j <= commands[i][1]; j++) { 
				list.add(array[j-1]);
				Collections.sort(list); // ArrayList 오름차순 정렬
			}
			answer[i] = list.get(commands[i][2] - 1); 
			list.clear();
		}

		return answer;
	}
}

💻 작성 코드 3 - java

  • Arrays.copyOfRange() 메소드 활용
  • copyOfRange(arr, n1, n2) (원본 배열, 복사할 시작 인덱스, 복사할 끝 인덱스)
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;
	}
}

🔗 문제 링크

[프로그래머스 - K번째수]
https://programmers.co.kr/learn/courses/30/lessons/42748

profile
ImOk👌

1개의 댓글

comment-user-thumbnail
2023년 3월 24일

잘보고 갑니다

답글 달기