-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrayMonotonic.js
More file actions
35 lines (30 loc) · 889 Bytes
/
Copy patharrayMonotonic.js
File metadata and controls
35 lines (30 loc) · 889 Bytes
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
/*
This implementation demonstrates how to
efficiently check whether a given array
is monotonic. Note that an array is said
to be monotonic if its elements from
left to right are either entirely
non-increasing or entirely non-decreasing.
Let n be the size of the array.
Time complexity: O(n)
Space complexity: O(1)
*/
function isMonotonic(array) {
if (!Array.isArray(array) || array.length <= 2) {
return true;
}
let isIncreasing = true,
isDecreasing = true;
for (let idx = 0; idx < array.length - 1; idx++) {
if (array[idx] < array[idx + 1]) {
isDecreasing = false;
}
if (array[idx] > array[idx + 1]) {
isIncreasing = false;
}
}
return isIncreasing || isDecreasing;
}
console.log(isMonotonic([1, 2, 4, 6])); // true
console.log(isMonotonic([6, 5, 4, 3])); // true
console.log(isMonotonic([1, 4, 2, 6])); // false