Longest window with range under a limit
Asked at Citadel, Two Sigma
Given a price series and a limit, find the length of the longest contiguous window in which the difference between the highest and lowest price is at most limit.
prices = [8, 2, 4, 7], limit = 4
-> 2 # windows [2,4] and [4,7] each have range <= 4
Return the maximum window length in .
Show a hint
You need the max and the min of the current window at once. Maintain two monotonic deques, and grow a right pointer while shrinking a left pointer whenever the range breaks the limit.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.