Stock Price Range over a Sliding Day Window
Problem statement
Return maximum minus minimum among the most recent k prices in stockPrices. If k exceeds the number of prices, use the full history. Return -1 for empty input.
Function
stockPriceRange(stockPrices: int[], k: int) → intExamples
Example 1
stockPrices = [4,1,8,2]k = 2return = 6The latest prices are 8 and 2.
Constraints
k > 0.- At most
10^5prices.