Professional Documents
Culture Documents
SubArray Problem
SubArray Problem
SubArray Problem
1. sliding window, where we shrink the window or reset the pointer base on certain rules
2. Sliding window + hashmap (strings /distinct/repeat character questions)
3. prefix sum + hashmap (subarray sum equals k, count number of subarrays xxx, maximum size
subarray equals k)
4. DP style, understanding a increment of i yields i+1 extra subarrays, understand Kadanes,
easiest to spot among all but hardest to implement sometimes (variant of LCS , 1D max min
store dp, stock games, xxx partitioning (i,j))
5. difference array, segment trees, for range query of range update advance questions
6. Greedy, the hardest to prove but most simple and elegant
7. Most unintuitive, monotonic queue (sliding window maximum) , monotonic stack (query of
length of continguous decreasing subarray)
8. Sorted arrays ~ Binary search
9. multiple sorted arrays ~ two pointers, compare and move pointers / heap queues
Try greedy or DP
BFS
BFS is used to find the smallest distance