Atmost Approach for Sliding Window

Learnings

Intuition

Basically Sliding Window
Does not give us Exact = k thing

Example

1 2 1 2 3

We want k=2 unique only in subarray

So 
    h
1 2 1 2 3
l

Idf, h++ we lose out 2,1 Which is potential answer
l++, we lose out 1 2 1 2

so,
Here's the trick

We get all subarrays, Having Atmost k,
Basically,
1 2 1 2
Is a match 
SO we take all
1 2 1 2, 2 1 2, 1 2, 2

And then we subtract appropriatey

So

Atmost k = Exact k + Atmost k-1

https://leetcode.com/problems/subarrays-with-k-different-integers/solutions/4944691/simplified-algorithm-explained-with-visual-example-c-java/

Last updated