Investable Periods 🐿️
Learn this problemProblem statement
A trading firm predicts the stock prices of a commodity for the next n days. A period of consecutive days is considered investable if the maximum price in the period is max_price, and the minimum price in the period is min_price. Find the number of investable periods in the next n days.
More formally, given an array price of length n, find the number of subarrays in which the maximum element is max_price and the minimum element is min_price.
Note: A subarray is a sequence of consecutive elements of the array.
Function
countInvestablePeriods(price: int[], max_price: int, min_price: int) → long
Complete the function countInvestablePeriods in the editor below.
countInvestablePeriods has the following parameters:
int price[n]: the predicted prices for the nextndaysint max_price: the maximum price of an investable periodint min_price: the minimum price of an investable period
Returns
long integer: the number of investable periods
Examples
Example 1
price = [4, 5, 3, 3, 1]max_price = 5min_price = 3return = 4Example 2
price = [2, 2, 1, 5, 1]max_price = 2min_price = 1return = 2Example 3
price = [1, 2, 3, 2]max_price = 3min_price = 2return = 3Constraints
1 ≤ n ≤ 10^51 ≤ price[i] ≤ 10^91 ≤ min_price ≤ max_price ≤ 10^9