일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- SECS
- 파이썬
- Baekjoon
- 프로그래머스
- 비트코인
- 백준
- 지브리필터
- Cars
- spring boot
- 암호화폐투자
- c
- Gem
- Digital Marketing
- CS
- modern c++
- finance & economics
- coins
- C++
- programmers
- python
- SECS-II
- 회원가입
- Spring
- SECS/GEM
- Investing
- Spring JPA
- 자바
- Computer Science
- ai이미지변환
- java
Archives
- Today
- Total
비버놀로지
[Programmers 프로그래머스] 42748 K번째수 본문
반응형
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
commands 에서 i=2, j=5, k=3 이면 배열에서 2번에서 5번 사이에서 3번째 값을 저장을 시키는 문제이다.
여기서 배열을 복사를 해주고, 복사한 배열을 정렬한후 3번째 값을 꺼낸다.
import java.util.Arrays;
public class K번째수 {
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 } };
int answer[] = new int[commands.length];
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];
}
System.out.println(Arrays.toString(answer));
}
}
반응형
'ALGORITM > JAVA' 카테고리의 다른 글
[Programmers 프로그래머스] 68935 3진법 뒤집기 (0) | 2021.01.07 |
---|---|
[Programmers 프로그래머스] 12901 2016년 (0) | 2021.01.07 |
[Programmers 프로그래머스] 42862 체육복 (0) | 2021.01.07 |
[Programmers 프로그래머스] 42840 모의고사 (0) | 2021.01.07 |
[Programmers 프로그래머스] 42576 완주하지 못한 선수 (0) | 2021.01.06 |
Comments