728x90
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Solution29_2 {
public static int[] solution(int[] array, int[][] commands) {
int[] answer = {};
ArrayList<Integer> list;
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < commands.length; i++) {
list = new ArrayList<>();
for (int j = commands[i][0] - 1; j < commands[i][1]; j++) {
list.add(array[j]);
}
Collections.sort(list);
result.add(list.get(commands[i][2] - 1));
}
answer = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
answer[i] = result.get(i);
}
return answer;
}
public static void main(String[] args) {
int array[] = { 1, 5, 2, 6, 3, 7, 4 };
int[][] commands = { { 2, 5, 3 }, { 4, 4, 1 }, { 1, 7, 3 } };
System.out.println(Arrays.toString(solution(array, commands)));
}
}
경험
인덱스는 0부터 시작이라서 문제에서 말하는 2~5까지 슬라이싱하는 게
1~4까지 슬라이싱을 하라는 뜻인 것에서 약간 버벅였다.
728x90
'Java 알고리즘 공부 (프로그래머스)' 카테고리의 다른 글
| [프로그래머스 Lv1] 덧칠하기 (Java) (1) | 2023.10.05 |
|---|---|
| [프로그래머스 Lv1] 예산 (Java) (0) | 2023.10.03 |
| [프로그래머스 Lv1] 소수 만들기 (Java) (0) | 2023.10.03 |
| [프로그래머스 Lv1] x만큼 간격이 있는 n개의 숫자 (Java) (0) | 2023.10.03 |
| [프로그래머스 Lv1] 핸드폰 번호 가리기 (Java) (0) | 2023.10.03 |