Mountain Gliding

You are given n mountains in a row, each with a height h[i]. You start gliding from any mountain.

You can glide from mountain a → b if:

  • height[a] > height[b]
  • Every mountain between a and b is lower than height[a].

Your task is to Find the maximum number of mountains you can visit on a route.

Example 1:

Input: n = 5 , h = [5,3,4,2,1]

Output: 4

Explanation: One optimal route:

5 → 4 → 2 → 1

Visited mountains = 4

Example 2:

Input: n = 4 , h = [10,7,8,5]

Output: 3

Explanation: Possible route:

10 → 8 → 5

Total visited = 3

Now Your Turn!

Pick the correct output for the given input

Input: n = 6 , h = [8,5,6,4,3,2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n ≤ 2×105
  • 1 ≤ h[i] ≤ 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int mountainRange(int n, vector<int>& heights) {
// Write your code here
return 0;
}
};
 
Test Case

Input:

H