1. Sliding Window 🪟
The Sliding Window pattern is used for problems involving contiguous subarrays or substrings. Think of it as a window that slides over your data to capture a specific range.
Source: Megan | DEV Community |
How to Identify:
- The problem involves finding a subarray or substring with specific properties (like maximum sum, minimum length, etc.).
Example Problems:
- Maximum Sum Subarray of Size K: Find the maximum sum of any contiguous subarray of size K.
- Longest Substring with K Distinct Characters: Find the length of the longest substring with at most K distinct characters.
- Minimum Window Substring: Find the smallest substring containing all characters of another string.
Diagram Explanation:
The diagram shows a sequence of numbers with a window moving across the sequence, highlighting the current window.
2. Two Pointer 👯
The Two-pointer pattern is helpful for problems involving sorted arrays or linked lists. It's like having two friends racing towards each other from opposite ends of the array.
Source: Sachintha Hewawasam | Dev Genius |
How to Identify:
- The problem involves finding pairs or subsets within a sorted array.
Example Problems:
- Pair with Target Sum: Find two numbers in a sorted array that add to a target sum.
- Triplet Sum to Zero: Find all unique triplets in the array that give the sum of zero.
- Container With Most Water: Find two lines that, together with the x-axis, form a container that holds the most water.
3. Binary Tree BFS 🌳
Binary Tree BFS (Breadth-First Search) is used for level order traversal of a tree. Imagine you're visiting nodes level by level, like walking down floors of a building.
Source: mishadoff.com |
How to Identify:
- The problem involves traversing a tree level by level.
Example Problems:
- Binary Tree Level Order Traversal: Return the level order traversal of a binary tree's nodes.
- Binary Tree Zigzag Level Order Traversal: Return a binary tree's zigzag level order traversal.
- Binary Tree Right Side View: Return the nodes visible from the right side view of a binary tree.
4. Topological Sort 📊
Topological Sort is used for problems involving tasks with dependencies. It's like creating a schedule where some tasks must be completed before others.
Source: Wangyy | Medium |
How to Identify:
- The problem involves ordering tasks or courses with prerequisites.
Example Problems:
- Course Schedule: Determine if you can finish all courses given the prerequisites.
- Alien Dictionary: A sorted dictionary of alien language is given, and the order of characters is found.
- Task Scheduling: Determine if tasks can be finished given their dependencies.
5. Binary Tree DFS 🌲
Binary Tree DFS (Depth-First Search) is used for a tree's pre-order, in-order, or post-order traversal. Think of it as diving deep into the tree before moving sideways.
Source: baeldung.com |
How to Identify:
- The problem involves traversing a tree deeply before backtracking.
Example Problems:
- Binary Tree Preorder Traversal: Return the preorder traversal of a binary tree's nodes.
- Binary Tree Inorder Traversal: Return the inorder traversal of a binary tree's nodes.
- Binary Tree Postorder Traversal: Return the postorder traversal of a binary tree's nodes.
6. Top K Elements 🥇
The Top K Elements pattern is used for problems that require finding the top K elements in a dataset. It's like determining the winners in a competition.
Source: byteinthesky.com |
How to Identify:
- The problem involves finding the largest, smallest, or most frequent elements.
Example Problems:
- Top K Frequent Elements: Find the k most frequent elements in an array.
- Kth Largest Element in an Array: Find the kth largest element in an unsorted array.
- K Closest Points to Origin: Find the k closest points to the origin in a 2D plane.
7. Modified Binary Search 🔍
Modified Binary Search is used for problems where you must find elements in a sorted array or range. It's like a regular binary search but with a twist!
Source: geeksforgeeks |
How to Identify:
- The problem involves finding elements or ranges in a sorted dataset.
Example Problems:
- Search in Rotated Sorted Array: Search for a target value in a rotated sorted array.
- Find Minimum in Rotated Sorted Array: Find the minimum element in a rotated sorted array.
- Peak Index in a Mountain Array: Find the peak element in a mountain array.
8. Subset 🌈
The Subset pattern is used for problems involving subsets, permutations, or combinations. Imagine generating all possible outfits from a set of clothes!
Source: geeksforgeeks |
How to Identify:
- The problem involves generating all possible combinations or subsets.
Example Problems:
- Subsets: Generate all possible subsets of a given set of numbers.
- Permutations: Generate all possible permutations of a given set of numbers.
- Combination Sum: Find all unique combinations that sum up to a target number.
These patterns are powerful tools in your problem-solving toolkit. Recognizing these patterns enables you to tackle a wide range of problems more efficiently. Happy coding! 😄
Feel free to share your thoughts and questions in the comments below! 🚀
#LeetCode #Algorithm #Programming Patterns