Count Maximum Profitable Groups π
Learn this problemProblem statement
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.
Function
countMaximumProfitableGroups(stockPrice: int[]) β long
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! π₯
Examples
Example 1
stockPrice = [3, 1, 3, 5]return = 10Example 2
stockPrice = [1, 5, 2]return = 5Example 3
stockPrice = [2, 3, 2]return = 5
Constraints
- 1 β€ n β€ 5 * 105
- 1 β€ stockPrice[i] β€ 108
More Amazon problems
- Secure Maximum DeliveriesOA Β· Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW Β· Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN Β· Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN Β· Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW Β· Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW Β· Seen Jul 2026
- Maximum System Memory CapacityOA Β· Seen Jul 2026
- Package Delivery SystemOA Β· Seen Jul 2026