Approach Summary
Keep at most 2 distinct fruit types in window. Shrink when the distinct count exceeds 2.
How to Recognize This Pattern
- At most k distinct elements in subarray
Complexity Analysis
Time Complexity
O(n)
Space Complexity
O(1)
Tags
Array Sliding Window Hash Map