Algorithm Patterns
Algorithm patterns are like special blueprints(templates) or structures we can use to solve specific type of problems.
They are like special tricks or methods that help us find solutions to different kinds of problems.
Below are some of the patterns:
1 Sliding Window Pattern
The sliding window pattern involves creating a window of a fixed size that moves across an array or string, performing a task for each subarray or substring within that window.
2 Two Pointers Pattern
The two pointers pattern involves using two pointers to traverse an array, usually starting at the ends and moving towards the middle, or starting at the middle and moving towards the ends.
3 Fast and Slow Pointers Pattern
Involves using two pointers to traverse a sequence at different speeds. Useful for finding a cycle in a linked list or for determining middle elements.
4 Merge Intervals Pattern
The merge intervals pattern is used to solve problems involving overlapping intervals. It typically involves sorting the intervals by their start times and then merging overlapping intervals.
5 Binary Search Pattern
The binary search pattern is used to efficiently search for an element in a sorted array or perform tasks that have a logarithmic time complexity.
6 Cyclic Sort Pattern
Used to sort an array of numbers where the numbers are in the range 1 to N and there are no duplicates.
7 Depth First Search (DFS)
Used to traverse or search tree or graph data structures. It starts at the root and explores as far as possible along each branch before backtracking.
8 Breadth First Search (BFS)
Used to traverse or search tree or graph data structures. It explores all neighbor nodes at the present depth prior to moving on the nodes at the next depth level.
9 Topological Sort Pattern
Used to order nodes in a directed graph where each nodes comes before all the nodes to which it has outbound edges.