-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9-Monolithic-Array.js
50 lines (43 loc) · 989 Bytes
/
9-Monolithic-Array.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
42
43
44
45
46
47
48
49
50
function isMovingUp(curr, next) {
return curr < next;
}
function isMovingDown(curr, next) {
return curr > next;
}
/*
*
* Use < or > instead >= and <=
* index < array.length-1
* count++ is source of truth
*
*/
function isMonotonic(array) {
// Write your code here
let direction = "";
if (array.length === 0 || array.length === 1) {
return true;
}
let count = 0;
for (let index = 0; index < array.length - 1; index++) {
if (isMovingUp(array[index], array[index + 1])) {
if (direction === "down") {
direction = "";
break;
} else {
direction = "up";
}
}
if (isMovingDown(array[index], array[index + 1])) {
if (direction === "up") {
direction = "";
break;
} else {
direction = "down";
}
}
count++;
}
return count === array.length - 1;
}
// Do not edit the line below.
exports.isMonotonic = isMonotonic;