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 |
Tags
- json-server
- 자바
- sw expert academy
- Algorithm
- JavaScript
- programmers
- redux-toolkit
- axios
- useDispatch
- react-redux
- 테코테코
- createSlice
- react
- Python
- redux-saga
- java
- maeil-mail
- 이코테
- 항해플러스
- redux
- C++
- 알고리즘
- 항해99
- SW
- Get
- 프로그래머스
- react-router
- 리액트
- 코딩테스트합격자되기
- 매일메일
Archives
- Today
- Total
Binary Journey
[프로그래머스] k번째 수 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
** Javascript
function solution(array, commands) {
var answer = [];
for (const command of commands) {
const sliced = array.slice(command[0]-1, command[1]);
sliced.sort(function(a,b){return a-b});
answer.push(sliced[command[2]-1]);
}
return answer;
}
** Python
def solution(array, commands):
answer = []
for i, j, k in commands: answer.append(sorted(array[i-1:j])[k-1])
return answer
** Java
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
int[] slice = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
Arrays.sort(slice);
answer[i] = slice[commands[i][2]-1];
}
return answer;
}
}
반응형
'프로그래머스 > level 1' 카테고리의 다른 글
[프로그래머스] 소수 만들기 (0) | 2021.11.01 |
---|---|
[프로그래머스] 완주하지 못한 선수 (0) | 2021.11.01 |
[프로그래머스] 모의고사 (0) | 2021.10.25 |
[프로그래머스] 나머지가 1이 되는 수 찾기 (0) | 2021.10.25 |
[프로그래머스] 폰켓몬 (0) | 2021.10.18 |