-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol38.js
More file actions
51 lines (44 loc) · 1.63 KB
/
sol38.js
File metadata and controls
51 lines (44 loc) · 1.63 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 문제 설명
// 배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
// 예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
// array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
// 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
// 2에서 나온 배열의 3번째 숫자는 5입니다.
// 배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때,
// commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
// 제한사항
// array의 길이는 1 이상 100 이하입니다.
// array의 각 원소는 1 이상 100 이하입니다.
// commands의 길이는 1 이상 50 이하입니다.
// commands의 각 원소는 길이가 3입니다.
function solution(array, commands) {
var answer = [];
commands.map((arr) => {
const start = arr[0] - 1;
const end = arr[1];
sort = array.slice(start, end).sort((a, b) => a - b);
answer.push(sort[arr[2] - 1]);
});
return answer;
}
console.log(
solution(
[1, 5, 2, 6, 3, 7, 4],
[
[2, 5, 3],
[4, 4, 1],
[1, 7, 3],
]
)
);
//[5, 2, 6, 3]
// console.log('prac',[1, 5, 2, 6, 3, 7, 4].slice(2,5)); //263
// console.log('prac',[1, 5, 2, 6, 3, 7, 4].slice(1,5)); //5263
// 다른 사람 풀이
function solution(array, commands) {
var answer = [];
answer = commands.map((a) => {
return array.slice(a[0] - 1, a[1]).sort((b, c) => b - c)[a[2] - 1];
});
return answer;
}