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
- 오버로딩
- this()
- FLUTTER
- 오버라이딩과 오버로딩 차이점
- 클래스 변수
- PriorityQueue
- stateful widget
- stateless widget
- 인스턴스 메서드
- 명예의전당(1)
- 참조형 반환타입
- static 메서드
- 조상의 생성자
- 운영체제의 구조
- 초기화 순서
- 운영체제란 무엇인가
- 호출스택
- stateful widget 생명주기
- 운영체제의 예
- static import문
- 객체 배열
- webview_flutter
- this()와 참조변수 this 차이점
- 기본형 매개변수
- 운영체제의 분류
- 프로그래머스
- 이것이코딩테스트다
- 참조형 매개변수
- 운영체제의 목적
- object클래스
Archives
- Today
- Total
Coram Deo
[프로그래머스] Lv1. K번째 수 본문
1. 나의 풀이
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
// array를 i부터 j까지 잘라서 다른 배열에 옮긴다.
// 새로운 array를 정렬한다.
// k번째 인덱스에 해당하는 새로운 array값을 answer배열에 넣는다.
int[] answer = new int[commands.length];
int j=0;
for(int[] command : commands){
int length = command[1] - command[0] + 1;
int[] newArray = new int[length];
for(int i=0; i<length; i++){
newArray[i] = array[command[0]-1+i];
}
Arrays.sort(newArray);
answer[j] = newArray[command[2]-1];
j++;
}
return answer;
}
}
2. Arrays.copyOfRange() 사용한 풀이
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
// Arrays.copyOfRange() 사용한 풀이
// array를 i부터 j까지 잘라서 다른 배열에 옮긴다.
// 새로운 array를 정렬한다.
// k번째 인덱스에 해당하는 새로운 array값을 answer배열에 넣는다.
int[] answer = new int[commands.length];
int index = 0;
for(int[] command:commands){
int i = command[0];
int j = command[1];
int k = command[2];
int[] newArray = Arrays.copyOfRange(array,i-1,j);
Arrays.sort(newArray);
answer[index] = newArray[k-1];
index++;
}
return answer;
}
}
'알고리즘 공부' 카테고리의 다른 글
[프로그래머스] Lv.1 기사단원의 무기 (0) | 2024.08.31 |
---|---|
[프로그래머스] Lv1. 문자열 내 마음대로 정렬하기 (0) | 2024.08.30 |
[프로그래머스] Lv1. 최소 직사각형 (0) | 2024.08.30 |
[프로그래머스] JadenCase 문자열 만들기(Java) - 리팩토링 과정 (0) | 2024.07.09 |
[백준] 1427. 소트인사이드 (0) | 2024.06.24 |