-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (37 loc) · 975 Bytes
/
index.js
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
/**
* @param {number[]} heights
* @returns boolean
*/
function checkJump(heights) {
const maxHeight = Math.max(...heights)
const maxHeightIndex = heights.indexOf(maxHeight) - 1
return (
heights[0] !== maxHeight &&
heights[heights.length - 1] !== maxHeight &&
heights
.slice(1)
.every((height, index) =>
index <= maxHeightIndex
? height >= heights[index]
: height <= heights[index]
)
)
}
/**
* @param {number[]} heights
* @returns boolean
*/
function checkJumpOptimized(heights) {
const maxHeightIndex = heights.indexOf(Math.max(...heights))
return (
maxHeightIndex > 0 &&
maxHeightIndex !== heights.length - 1 &&
heights
.slice(1, maxHeightIndex + 1)
.every((height, index) => height >= heights[index]) &&
heights
.slice(maxHeightIndex + 1)
.every((height, index) => height <= heights[index + maxHeightIndex])
)
}
export {checkJump, checkJumpOptimized}