diff --git a/coding_interviews/algoexpert/longest-peak/longest-peak.js b/coding_interviews/algoexpert/longest-peak/longest-peak.js new file mode 100644 index 0000000..32f4565 --- /dev/null +++ b/coding_interviews/algoexpert/longest-peak/longest-peak.js @@ -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; +}