반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Trino
- 코테
- Iceberg
- hadoop
- 알고리즘
- 자바
- bigdata engineer
- 맛집
- 삼성역맛집
- 코딩테스트
- apache iceberg
- HIVE
- BigData
- 코엑스
- BFS
- 코딩
- bigdata engineering
- 양평
- Data Engineer
- 프로그래머스
- java
- 백준
- 영어
- 개발
- Data Engineering
- Spark
- 여행
- dfs
- 코엑스맛집
- 용인맛집
Archives
- Today
- Total
지구정복
[정렬] 프로그래머스 - K번째 수 본문
728x90
반응형
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
내 풀이
더보기
import java.util.ArrayList;
import java.util.Collections;
class Solution {
public int[] solution(int[] array, int[][] commands) {
ArrayList<Integer> result = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
for( int i=0; i<commands.length; i++ ) {
for( int j=commands[i][0]; j<=commands[i][1]; j++ ) {
temp.add( array[j-1] );
}
Collections.sort( temp );
result.add( temp.get( commands[i][2]-1 ) );
temp.clear();
}
int[] answer = new int[ result.size() ];
for( int i=0; i<result.size(); i++ ) {
answer[i] = result.get(i);
}
return answer;
}
}
다른 사람 풀이
더보기
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
//배열 복사 메서드를 이용하여 i~j만큼 잘러서 복사하고
//복사된 배열을 정렬한다.
for(int i=0; i<commands.length; i++){
int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(temp);
answer[i] = temp[commands[i][2]-1];
}
return answer;
}
}
728x90
반응형
'데이터 엔지니어링 정복 > Algorithm' 카테고리의 다른 글
[Greedy] 백준 - 강의실배정 (0) | 2021.06.04 |
---|---|
[Greedy] 백준 - 신입사원 (0) | 2021.06.03 |
[Greedy] 백준 - 동전0 (0) | 2021.06.03 |
[완전탐색] 프로그래머스 - 모의고사 (0) | 2021.04.11 |
[해쉬] 프로그래머스 - 완주하지 못한 선수 (0) | 2021.04.11 |
Comments