반응형
Notice
Recent Posts
Recent Comments
Link
Completion over Perfection
프로그래머스 - K번째 수 (자바 JAVA 풀이) 본문
반응형

Arrays.sort 메소드로 풀었습니다.
자세한 풀이는 아래 코드 및 주석내용 참고해주세요.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import java.util.*;
class Solution {
static int arr [];
static int sorted_arr [];
static int answer [];
public int[] solution(int[] array, int[][] commands) {
answer = new int [commands.length];
for(int i=0; i<commands.length; i++){
arr = new int[3]; // commands 배열의 개별 배열을 담을 공간 생성
sorted_arr = new int [array.length+1];
arr = commands[i];
int start, end, target;
start = arr[0]; // 2
end = arr[1]; // 5
target = arr[2]; // 3
int diff = end - start + 1; // 자를 구간의 크기 = 4
sorted_arr = new int [diff];
for(int j=0; j<diff; j++){ // j는 1부터 4까지
sorted_arr[j] = array[start+j-1];
}
Arrays.sort(sorted_arr);
answer[i] = sorted_arr[target-1];
}
return answer;
}
}
|
cs |
반응형
'자바 (Java)' 카테고리의 다른 글
| 백준 2470 - 두 용액 (JAVA 자바 풀이) (0) | 2023.03.02 |
|---|---|
| 백준 1806 - 부분합 (자바 JAVA) (0) | 2023.02.25 |
| 백준 20040 - 사이클 게임 (자바 JAVA) (0) | 2023.02.19 |
| 백준 25083 - 새싹 (JAVA 자바) (0) | 2023.02.18 |
| 백준 10172 - 개 (JAVA 자바) (0) | 2023.02.17 |
Comments