Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jan 2, 2024
1 parent 8bd3b68 commit 51a1a8e
Showing 1 changed file with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@
// Space: O(1)

function isMonotonic(array) {
let num = array[0];
let upward;
let isNonDecreasing = true;
let isNonIncreasing = true;

for (let index = 1; index < array.length; index++) {
if (upward === undefined) {
upward = array[index] !== num ? array[index] > num : undefined;
}

if (upward !== undefined && upward && array[index] < num) {
return false;
}

if (upward !== undefined && !upward && array[index] > num) {
return false;
}

num = array[index];
for (let index = 0; index < array.length - 1; index++) {
if (array[index] > array[index + 1]) isNonDecreasing = false;
if (array[index] < array[index + 1]) isNonIncreasing = false;
}

return true;
return isNonDecreasing || isNonIncreasing;
}

0 comments on commit 51a1a8e

Please sign in to comment.