https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
- 첫 번째 풀이 (배열을 직접 자름)
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 size = commands[i][1]-commands[i][0]+1;
int[] nums = new int[size];
int x = 0;
for(int j = commands[i][0]-1; j < commands[i][1]; j++){
nums[x] = array[j];
x++;
}
Arrays.sort(nums);
answer[i] = nums[commands[i][2]-1];
}
return answer;
}
}
/*
1. commands의 크기만큼 반복
2. 각 반복문마다 자른 배열을 생성하고, i,j,k값에 따른 값을 구하고
3. 하나씩 answer배열에 넣기
*/
- 두 번째 풀이 (배열을 함수를 이용해 자름)
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[] nums = Arrays.copyOfRange(array, commands[i][0] - 1, commands[i][1]);
Arrays.sort(nums);
answer[i] = nums[commands[i][2] - 1];
}
return answer;
}
}
/*
1. commands의 크기만큼 반복
2. 각 반복문마다 자른 배열을 생성하고, i,j,k값에 따른 값을 구하고
3. 하나씩 answer배열에 넣기
*/
'코딩테스트 > 코딩테스트 문제풀이' 카테고리의 다른 글
| [Java] 프로그래머스 <N개의 최소공배수> (0) | 2025.02.11 |
|---|---|
| [Java] 프로그래머스 <귤 고르기> (0) | 2025.02.09 |
| [Java] 프로그래머스 <폰켓몬> (0) | 2025.02.05 |
| [Java] 프로그래머스 <같은 숫자는 싫어> (0) | 2025.02.04 |
| [Java] 프로그래머스 <조이스틱> (0) | 2025.02.04 |