프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
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입니다.
풀이
정렬할 부분만 가져와서 배열에 저장하고, 배열을 정렬하고, 배열의 k번째 수를 정답배열에 넣으면 된다.
- array의 i번째부터 j번째까지 숫자를 배열(tmp)에 저장
- 배열 tmp를 정렬
- 배열 tmp의 k번째 수를 정답배열 answer에 넣기.
코드 1 - 오답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(0)];
// array의 i번째부터 j번째까지 숫자를 배열(tmp)에 저장
// 배열 tmp를 정렬
// 배열 tmp의 k번째 수를 정답배열 answer에 넣기.
for (int x = 0; x < commands.GetLength(0); x++)
{
int[] commandArray = new int[commands.GetLength(0)];
for (int xx = 0; xx < commands.GetLength(0); xx++)
{
commandArray[xx] = commands[x, xx];
}
// int[] commandArray = commands[x];
int i = commandArray[0] - 1;
int j = commandArray[1] - 1;
int k = commandArray[2] - 1;
int[] tmp = new int[j - i + 1];
int idx = 0;
for (int y = i; y <= j; y++)
{
tmp[idx++] = array[y];
}
Array.Sort(tmp);
int target = tmp[k];
answer[x] = target;
}
return answer;
}
}
런타임 에러가 났다.
프로그래머스 AI 코드 피드백을 받아보니 내 코드에서 GetLength를 잘못 쓰고 있다고 한다.
다시 보니 무조건 행을 기준으로 반복문을 돌리고 있었다.
코드 2 - 오답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(0)];
// array의 i번째부터 j번째까지 숫자를 배열(tmp)에 저장
// 배열 tmp를 정렬
// 배열 tmp의 k번째 수를 정답배열 answer에 넣기.
for (int x = 0; x < commands.GetLength(0); x++)
{
int[] commandArray = new int[commands.GetLength(0)];
for (int xx = 0; xx < commands.GetLength(1); xx++)
{
commandArray[xx] = commands[x, xx];
}
// int[] commandArray = commands[x];
int i = commandArray[0] - 1;
int j = commandArray[1] - 1;
int k = commandArray[2] - 1;
int[] tmp = new int[j - i + 1];
int idx = 0;
for (int y = i; y <= j; y++)
{
tmp[idx++] = array[y];
}
Array.Sort(tmp);
int target = tmp[k];
answer[x] = target;
}
return answer;
}
}
런타임 에러가 났다.
AI 코드 피드백을 받아보니 아직 GetLength를 잘못 쓰고 있다고 한다.
다시 보니 commandArray도 행 길이로 배열 크기를 지정했다. commandArray는 명령어들을 저장해야 하므로 길이는 무조건 3이다. commandArray 길이를 3으로 설정해도 되긴 하는데 GetLength(1)로 설정해도 되겠다. 3이 결국 열의 길이이므로.
코드 3 - 정답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(0)];
// array의 i번째부터 j번째까지 숫자를 배열(tmp)에 저장
// 배열 tmp를 정렬
// 배열 tmp의 k번째 수를 정답배열 answer에 넣기.
for (int x = 0; x < commands.GetLength(0); x++)
{
int[] commandArray = new int[commands.GetLength(1)];
for (int xx = 0; xx < commands.GetLength(1); xx++)
{
commandArray[xx] = commands[x, xx];
}
// int[] commandArray = commands[x];
int i = commandArray[0] - 1;
int j = commandArray[1] - 1;
int k = commandArray[2] - 1;
int[] tmp = new int[j - i + 1];
int idx = 0;
for (int y = i; y <= j; y++)
{
tmp[idx++] = array[y];
}
Array.Sort(tmp);
int target = tmp[k];
answer[x] = target;
}
return answer;
}
}
복습1 풀이 (5/10)
array 길이는 최대 100
O(N^2) 풀이로 푼다해도 연산은 10000번.
O(N^2) 풀이로 풀어도 되겠다. 이중 for문을 사용해서 풀어보자.
코드1 - 오답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(1)];
int answerIndex = 0;
for(int x = 0; x < commands.GetLength(0); x++)
{
int i = commands[x, 0];
int j = commands[x, 1];
int k = commands[x, 2];
int[] tmp = new int[j - i + 1];
int index = 0;
for(int y = i-1; y <= j-1; y++)
{
tmp[index++] = array[y];
}
Array.Sort(tmp);
answer[answerIndex++] = tmp[k-1];
}
return answer;
}
}
answer 배열 크기를 commands 배열의 열 길이로 설정했다.
그러면 answer 배열 크기는 항상 3이다.
만약 명령어가 3개보다 더 많으면 answer 배열 크기가 3이라서 4번째, 5번째 명령어 결과값을 저장할 수 없다.
만약 명령어가 3개보다 적으면 answer 배열에는 정답 결과값과 0이 저장된다.
answer 배열 크기를 수정해야 한다.
코드2 - 정답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(0)];
int answerIndex = 0;
for(int x = 0; x < commands.GetLength(0); x++)
{
int i = commands[x, 0];
int j = commands[x, 1];
int k = commands[x, 2];
int[] tmp = new int[j - i + 1];
int index = 0;
for(int y = i-1; y <= j-1; y++)
{
tmp[index++] = array[y];
}
Array.Sort(tmp);
answer[answerIndex++] = tmp[k-1];
}
return answer;
}
}
answer 배열 크기는 명령어의 개수로 설정해야 한다.
commands.GetLength(1)은 commands 배열의 열 크기다. 이 문제에서 열은 항상 3개이다.
commands.GetLength(0)은 commands 배열의 행 크기다. 행 크기가 곧 명령어의 개수이므로 answer 배열 크기는 commands.GetLength(0)으로 설정해야 한다.
answer 배열 크기를 commands.GetLength(0)으로 설정하니 해결되었다.
복습1 풀이 (6/5)
문제
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬하고, k번째 있는 수를 구하기.
(문제에서 말하는 2번째는 실제로는 1번째를 뜻함)
배열 array와 [i,j,k]를 원소로 가진 2차원 배열 commands가 주어질 때,
commands[x]의 결과들을 배열에 담아 반환해라.
제한사항
- 1 <= array 길이 <= 100
- 1 <= array 원소 <= 100
- 1 <= commands 길이 <= 50
- commands 각 원소 길이 == 3
풀이
- 정답 배열 생성 (크기는 commands.GetLength(0))
- commands[x]번째를 가져와서 i, j, k를 저장하기.
- j-i+1 크기의 arr 배열 만들기.
- arr 배열에 array배열의 i-1번째부터 j-1번째까지 넣기.
- arr 배열 정렬하기.
- arr 배열의 k번째 원소를 정답 배열에 넣기.
- 정답 배열 크기는 commands. 명령어 개수.
코드 1 - 정답
public int[] solution(int[] array, int[,] commands)
{
int[] answer = new int[commands.GetLength(0)];
for(int x = 0 ; x < commands.GetLength(0); x++)
{
int i = commands[x, 0];
int j = commands[x, 1];
int k = commands[x, 2];
int[] arr = new int[j-i+1];
int arrIndex = 0;
for(int y = i-1; y <= j-1; y++)
arr[arrIndex++] = array[y];
Array.Sort(arr);
answer[x] = arr[k-1];
}
return answer;
}
코드 2 - 정답
using System;
public class Solution {
public int[] solution(int[] array, int[,] commands) {
int[] answer = new int[commands.GetLength(0)];
for(int x = 0 ; x < commands.GetLength(0); x++)
{
int i = commands[x, 0] - 1;
int j = commands[x, 1] - 1;
int k = commands[x, 2] - 1;
int[] arr = new int[j-i+1];
for(int y = 0; y < arr.Length; y++)
arr[y] = array[i + y];
Array.Sort(arr);
answer[x] = arr[k];
}
return answer;
}
}
i, j, k를 초기화할때 처음부터 -1을 해주면 y, k의 인덱스를 지정할 때 -1을 하지 않아도 된다.
'자료구조, 코딩테스트 > 정렬(Sort)' 카테고리의 다른 글
| [코딩테스트] Programmers - 문자열 내 마음대로 정렬하기 (실패) (0) | 2026.05.23 |
|---|---|
| [코딩테스트] Programmers - 가장 큰 수 (실패) (0) | 2026.05.22 |
| [자료구조] Radix Sort (0) | 2026.05.02 |
| [자료구조] Counting Sort (0) | 2026.05.01 |
| [자료구조] Selection Sort, Bubble Sort, Merge Sort, Quick Sort (0) | 2026.04.25 |