Count Maximum Profitable Groups π
A team of analysts at Amazon needs to analyze the stock prices of Amazon over a period of several months.
A group of consecutively chosen months is said to be maximum profitable if the price in its first or last month is the maximum for the group. More formally, a group of consecutive months [l, r] (1 β€ l β€ r β€ n) is said to be maximum profitable if either:
Given prices over n consecutive months, find the number of maximum profitable groups which can be formed. Note that the months chosen must be consecutive, i.e., you must choose a subarray of the given array.
Complete the function countMaximumProfitableGroups function in the editor below.
countMaximumProfitableGroups has the following parameter:
int stockPrice[n]: the stock prices
Returns
long integer: the number of maximum profitable groups
π 1000 thanks to spike for spike's incredible help! π₯
1Example 1
2Example 2
3Example 3

Constraints
Limits and guarantees your solution can rely on.
- 1 β€ n β€ 5 * 105
- 1 β€ stockPrice[i] β€ 108