Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jan 4, 2024
1 parent 40c4a06 commit 4ab4f58
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions coding_interviews/algoexpert/longest-peak/longest-peak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Runtime: O(N)
// Space: O(1)

function longestPeak(array) {
let longestPeak = 0;
let leftEdge;
let rightEdge;

for (let peakIndex = 1; peakIndex < array.length - 1; peakIndex++) {
if (
array[peakIndex] > array[peakIndex - 1] &&
array[peakIndex] > array[peakIndex + 1]
) {
for (let index = peakIndex; index > 0; index--) {
if (array[index - 1] < array[index]) {
leftEdge = index - 1;
} else {
break;
}
}

for (let index = peakIndex; index < array.length - 1; index++) {
if (array[index] > array[index + 1]) {
rightEdge = index + 1;
} else {
break;
}
}

longestPeak = Math.max(longestPeak, rightEdge - leftEdge + 1);
}
}

return longestPeak;
}

0 comments on commit 4ab4f58

Please sign in to comment.