Algorithms guide Techniques A Two pointers B Fast and slow pointers The pointers can be used to traverse the array or list in either direction however one moves faster than the other Generally the slow pointer moves forward by a factor of one and the fast
Techniques A Two pointers B Fast and slow pointers The pointers can be used to traverse the array or list in either direction however one moves faster than the other Generally the slow pointer moves forward by a factor of one and the fast pointer moves by a factor of two in each step However the speed can be adjusted according to the problem statement Unlike the two-pointers approach which is concerned with data values the fast and slow pointers approach is used to determine data structure traits using indices in arrays or node pointers in linked lists The approach is commonly used to detect cycles in the given data structure so it ? s also known as Floyd ? s cycle detection algorithm The key idea is that the pointers start at the same location but move forward at di ?erent speeds If there is a cycle the two are bound to meet at some point in the traversal To understand the concept think of two runners on a track While they start from the same point they have di ?erent running speeds If the racetrack is a circle the faster runner will overtake the slower one after completing a lap On the other hand if the track is straight the faster runner will end the race before the slower one hence never meeting on the track again The fast and slow pointers pattern uses the same intuition C
Documents similaires
-
32
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Aucune attribution requise- Détails
- Publié le Mar 05, 2022
- Catégorie Heavy Engineering/...
- Langue French
- Taille du fichier 18.5kB