Compute streaming sliding-window minimums
Problem Description
You are provided with an integer array nums of length n and an integer k, representing the window size.
Your task is to process the nums array in a streaming manner, from left to right. For each index i (from 0 to n-1), you must determine and output the minimum value within the most recent window of up to k elements that ends at i.
More formally, for each index i:
- Let
L = max(0, i - k + 1). - The active window is defined as the subarray
nums[L..i]. - The required output for index
iisans[i] = min(nums[L..i]).
The final deliverable is an array ans of length n, where ans[i] contains the minimum value for the window ending at i.
Example Clarification
If nums contains 10 values and k = 3, your algorithm must produce 10 outputs, one for each element, corresponding to the minimum in the window ending at that element. This is distinct from returning n-k+1 outputs, which would only cover full windows.
Constraints
- The length of the array
nwill be between1and2 * 10^5. - The window size
kwill be between1and10^5. - Each element
nums[i]will fit within a 32-bit signed integer.
Performance Goal
Design an algorithm that operates in a streaming fashion (meaning ans[i] is produced as soon as nums[i] is processed) and achieves an overall time complexity of O(n).
AI interview prep
Core dilemma
The central engineering conflict is to efficiently maintain the minimum element within a dynamically shifting window of `k` elements, ensuring `O(1)` amortized time per element to achieve the overall `O(N)` goal. This requires a data structure that can quickly add elements to one end, remove elements from the other end, and query the minimum, all while keeping the structure lean.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute streaming sliding-window minimums
Problem Description
You are provided with an integer array nums of length n and an integer k, representing the window size.
Your task is to process the nums array in a streaming manner, from left to right. For each index i (from 0 to n-1), you must determine and output the minimum value within the most recent window of up to k elements that ends at i.
More formally, for each index i:
- Let
L = max(0, i - k + 1). - The active window is defined as the subarray
nums[L..i]. - The required output for index
iisans[i] = min(nums[L..i]).
The final deliverable is an array ans of length n, where ans[i] contains the minimum value for the window ending at i.
Example Clarification
If nums contains 10 values and k = 3, your algorithm must produce 10 outputs, one for each element, corresponding to the minimum in the window ending at that element. This is distinct from returning n-k+1 outputs, which would only cover full windows.
Constraints
- The length of the array
nwill be between1and2 * 10^5. - The window size
kwill be between1and10^5. - Each element
nums[i]will fit within a 32-bit signed integer.
Performance Goal
Design an algorithm that operates in a streaming fashion (meaning ans[i] is produced as soon as nums[i] is processed) and achieves an overall time complexity of O(n).