AmazonOA #361
Count Maximum Profitable Groups
Estimated HardJudge-ready
Topics (AI-labelled)Stacks & Heaps
Count the non-empty contiguous subarrays whose first or last element equals the maximum of the subarray. Equal and repeated values are allowed, and a range that satisfies both ends is counted only once.
Input
The first line contains n and the second line contains n prices. The original bounds are 1≤n≤500000, 1≤a[i]≤10^8.
Output
Output a single integer, the answer.
Example 1
Input:
4
3 1 3 5
Output:
10
Example 2
Input:
3
1 5 2
Output:
5
Example 3
Input:
3
3 1 3
Output:
6
Example notes
The outputs of the three public examples are 10; 5; and 6, checked by independent enumeration or direct simulation.
Editorial
Loading solution…