15% off all plans & roadmaps Use code
See plansLeetCode 845 · Solution & editorial
You may recall that an array arr is a mountain array if and only if: arr.length >= 3 There exists some index i (0-indexed) with 0 arr[i + 1] > ... > arr[arr.length - 1] Given an integer array arr, return the length of the longest subarray, which is a mountain. Return 0 if there is no mountain subarray.
Longest Mountain in Array is a Array/Matrix Manipulation Patterns problem, in the Product Except Self (Prefix/Suffix Products) subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
Master the prefix and suffix products technique to calculate product of array except self without division.
The reference solution runs in O(n) time and O(1) space.
Longest Mountain in Array has been reported in interviews at Amazon, Paypal.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Longest Mountain in Array is a Array/Matrix Manipulation Patterns problem, in the Product Except Self (Prefix/Suffix Products) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.