22. Sliding Window & 2 pointer

4 PATTERNS IN SLIDING WINDOW QUESTION-

  • constant window
  • longest subarray
  • no of subarrays
  • shotest window (imp)--when u r not sure of shrinking/ expanding

-----------------------------------------------------------------------------------------------------------

1.) Constant Window-

e.g., subarray with max sum and length 'k'

-------------------------------------------------------------------------------------------------------------------------

2.) Longest subarray or substring problem with a condition-

go from brute-->better-->optimal

e.g., longest subarray with sum<k

--------

BETTER- [ time- O(2N) ]

1. start with window of size 1

2. expand and shrink window size

3. when condition violated shrink till condition again becomes true

-------------

OPTIMAL- ( time-O(N) )

shrinking ko khatam krke O(n) me krenge...

shrinking bahut barr krne se acha ek baar krdo bas 

BUT, if asked to print all subarrays then nothing can be done to reduce 2n

-------------------------------------------------------------------------------------------------------------------------

3.) No.Of Subarrays-(with given cond)

-------------------------------------------------------------------------------------------------------------------------
4.) Finding Shortest / minimum window with a given condition (RARE)

here firstly we will find a possible window and then try to shrink it to get an answer.

---------------------------------------------------------------------------------------------------------------------------
QUESTIONS-

1.) LONGEST  SUBSTRING  WITHOUT  REPEATING  CHARACTERS-

Brute- (n^2 )mein krdo using hash array

Optimal-

-----------------------------------------------------------------------------------------------------------------------

2.) Consecutive Ones-
Better-
mere submission me while loop lagado instead of a vector use zeros variable

OPTIMAL-









---------------------------------------------------------------------------------------------------------------------------

3. ) Fruits into Baskets-

Longest subarray with atmost k distinct elements

BRUTE- (n^2)   saare subarrays lelo, i.e., a[0] se bharne start kro ek set m aur len ka count kro jaise hi set m                   2 se jyada elmts aaye ruk jaao aur a[1] se start kro and so on...

BETTER- (2n)--    sliding window


   --------------------------------------------------------------------------------------------------------------------------

4.) Substring containing all 3 characters-

BRUTE-

minor optimisation-

-----------------------

finding minimal valid window-


----------------------------------------------------------------------------------------------------------------------

5. ) Longest repeating character replacement-

BRUTE-  
count in each substring ki jitne char change krne vo number k se chota hai kya
BETTER- 


---------------------------------------------------------------------------------------------------------------------------

6.) COUNT no of Binary Subarrays with Sum k-

with sum k wale = (with sum<=k wale) + (with sum <= (k-1) wale)

with sum <=k walo me agar mera sum ni badalra even after r++ and condn of sum<=k is also satisfied then cnt ko utne se badha do jitne subarrays possible hain till the current r, and that is nothing but          r-l+1 like i=2 pe ho toh 3 possible hain...


-------------------------------------------------------------------------------------------------------------------------

7.) Subarrays with K different integers-

Brute-


Better-

----------------------------------------------------------------------------------------------------------------------

8.) Minimum window substring-

BRUTE-








---------------------------------------------------------

BETTER-


---------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

18. greedy algorithm

19. strings (LB)

17. BINARY SEARCH on 2d arrrays