Advertisement

Longest Mountain in Array - LeetCode 845 Solution

Longest Mountain in Array - Complete Solution Guide

Longest Mountain in Array is LeetCode problem 845, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 < i < arr.length - 1 such that: arr[0] < arr[1] < ... < arr[i - 1] < arr[i] arr[i] > 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. Example 1: Input: arr = [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which ha

Detailed Explanation

The problem asks us to find the length of the longest "mountain" subarray within a given integer array `arr`. A mountain subarray is defined as a subarray of at least length 3 where there exists an index `i` such that the elements strictly increase up to `i` and strictly decrease after `i`. In simpler terms, a mountain has a peak, with elements increasing to the left of the peak and decreasing to the right. If no such mountain exists, the function should return 0.

Solution Approach

The solution uses a single-pass approach with a sliding window technique. It iterates through the array, identifying potential starting points of a mountain. For each starting point, it attempts to find an increasing sequence followed by a decreasing sequence. If both sequences are found, it calculates the length of the mountain and updates the maximum length found so far. If either the increasing or decreasing sequence is missing, the algorithm moves to the next potential starting point.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_len` to 0 to store the length of the longest mountain found so far and `i` to 0 to iterate through the array.
  2. Step 2: The outer `while` loop iterates through the array while `i` is less than `n` (the length of the array).
  3. Step 3: `base` is assigned the value of `i`. This marks the potential start of a mountain.
  4. Step 4: The inner `while` loop finds the increasing slope (upslope) by incrementing `i` as long as `i + 1` is within bounds and `arr[i] < arr[i+1]`.
  5. Step 5: After finding the upslope, check if `i > base`. If it's true, an upslope was found. Set `peak = i`.
  6. Step 6: The next inner `while` loop finds the decreasing slope (downslope) by incrementing `i` as long as `i + 1` is within bounds and `arr[i] > arr[i+1]`.
  7. Step 7: After finding the downslope, check if `i > peak`. If true, both upslope and downslope were found, meaning a valid mountain exists.
  8. Step 8: If a valid mountain exists, calculate its length (`i - base + 1`) and update `max_len` if the current mountain is longer than the previous `max_len`.
  9. Step 9: If `i == base`, it means no upslope was found, so increment `i` to move to the next element. This avoids getting stuck in an infinite loop when the current element is not the start of any ascending sequence.
  10. Step 10: Repeat steps 3-9 until the entire array has been processed. Return `max_len`.

Key Insights

  • Insight 1: Iterate through the array and identify potential 'peaks' which form the mountain's apex.
  • Insight 2: Use two pointers, one to find the increasing slope leading up to the peak and another to find the decreasing slope after the peak.
  • Insight 3: Handle cases where there is only an increasing or decreasing sequence, or where no increasing/decreasing sequence exists at all.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Dynamic Programming, Enumeration.

Companies

Asked at: Databricks, Faire, SoFi.