티스토리 뷰
https://programmers.co.kr/learn/courses/30/lessons/42748
코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
[풀이 과정]
1. 여러 개의 명령어(i, j, k)가 들어 올 수 있으므로, 기존의 배열을 수정하면 안된다.
2. 명령어의 길이는 항상 3이므로, 예외 처리를 할 필요가 없다.
3. i부터 j까지 배열을 복사하고 정렬한다.
4. 정렬한 배열에서 k번째 수를 찾는다.
[소스 코드]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class KthNumber {
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)));
}
private static int[] solution(int[] array, int[][] commands) {
List<Integer> temps = new ArrayList<>();
for (int[] command : commands) {
int[] temp = Arrays.copyOfRange(array, command[0] - 1, command[1]);
Arrays.sort(temp);
temps.add(temp[command[2] - 1]);
}
int[] answer = new int[temps.size()];
for (int i = 0; i < answer.length; i++) {
answer[i] = temps.get(i);
}
return answer;
}
}
'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 2016년 (lv.1) (0) | 2020.06.25 |
---|---|
[프로그래머스] 체육복 (lv.1) (0) | 2020.06.24 |
[프로그래머스] 모의고사 (lv.1) (0) | 2020.06.23 |
[프로그래머스] 완주하지 못한 선수 (lv.1) (0) | 2020.06.23 |
[프로그래머스] 크레인 인형뽑기 게임 (lv.1) (0) | 2020.06.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 자료구조
- BOJ
- 단계별로 문제풀이
- 열혈강의
- repository
- 깃
- 2020 카카오 인턴십
- 백준
- binary search
- 그래프
- 이것이 코딩테스트다
- Algorithm
- 그리디
- git
- 알고리즘
- spring boot 2.3.1
- 구현
- dfs
- programmers
- Algorihtm
- Summer/Winter Coding(~2018)
- 정렬
- OS
- 코틀린
- Idempotent
- bfs
- DP
- 2019 카카오 개발자 겨울 인턴십
- 저장소
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함