bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 845. Longest Mountain in Array

845. Longest Mountain in Array

Leetcode Solutions

Two Pointer Approach for Longest Mountain in Array

  1. Initialize the longest mountain length to 0.
  2. Use a base pointer to iterate through the array, starting from index 0.
  3. For each base, if it's a valid starting point (arr[base] < arr[base + 1]), try to find the peak of the mountain.
  4. Move the end pointer up as long as arr[end] < arr[end + 1] to find the peak.
  5. Once the peak is found, continue moving the end pointer down as long as arr[end] > arr[end + 1] to find the end of the mountain.
  6. Update the longest mountain length if the current mountain is longer.
  7. Move the base pointer to the end of the current mountain or to the next potential starting point.
  8. Repeat steps 3-7 until the end of the array is reached.
  9. Return the longest mountain length.
UML Thumbnail

Dynamic Programming Approach for Longest Mountain in Array

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...