|
1 | 1 | # Patterns for Problem Solving
|
2 | 2 |
|
3 | 3 | ## Pattern: Two Pointers
|
4 | | -**Description:** This method uses two pointers to traverse an array or a list from different ends or directions. |
5 | | -**Usage:** It's particularly useful for ordered data structures, where we can make intelligent decisions based on the position of the pointers. |
6 | | -**Problems:** 'Pair with Target Sum', 'Remove Duplicates', 'Squaring a Sorted Array'. |
| 4 | +**Description:** This method uses two pointers to traverse an array or a list from different ends or directions. |
| 5 | +**Usage:** It's particularly useful for ordered data structures, where we can make intelligent decisions based on the position of the pointers. |
| 6 | +**Problems:** 'Pair with Target Sum', 'Remove Duplicates', 'Squaring a Sorted Array'. |
7 | 7 |
|
8 | 8 | ## Pattern: Island (Matrix Traversal)
|
9 | | -**Description:** It involves traversing a matrix to find 'islands' or contiguous groups of elements. |
10 | | -**Usage:** It's generally used in grid-based problems, especially when we need to group connected elements together. |
11 | | -**Problems:** 'Number of Islands', 'Max Area of Island', 'Flood Fill'. |
| 9 | +**Description:** It involves traversing a matrix to find 'islands' or contiguous groups of elements. |
| 10 | +**Usage:** It's generally used in grid-based problems, especially when we need to group connected elements together. |
| 11 | +**Problems:** 'Number of Islands', 'Max Area of Island', 'Flood Fill'. |
12 | 12 |
|
13 | 13 | ## Pattern: Fast & Slow Pointers
|
14 | | -**Description:** In this method, two pointers move at different speeds in a data structure. |
15 | | -**Usage:** It is commonly used to detect cycles in a structure, find middle elements, or to solve other specific problems related to linked lists. |
16 | | -**Problems:** 'LinkedList Cycle', 'Middle of the LinkedList', 'Palindrome LinkedList'. |
| 14 | +**Description:** In this method, two pointers move at different speeds in a data structure. |
| 15 | +**Usage:** It is commonly used to detect cycles in a structure, find middle elements, or to solve other specific problems related to linked lists. |
| 16 | +**Problems:** 'LinkedList Cycle', 'Middle of the LinkedList', 'Palindrome LinkedList'. |
17 | 17 |
|
18 | 18 | ## Pattern: Sliding Window
|
19 | | -**Description:** This pattern involves creating a 'window' into the data structure and then moving that window around to gather specific information. |
20 | | -**Usage:** Mostly used in array or list-based problems where you need to find a contiguous subset that fulfills certain conditions. |
21 | | -**Problems:** 'Maximum Sum Subarray of Size K', 'Smallest Subarray with a given sum', 'Longest Substring with K Distinct Characters'. |
| 19 | +**Description:** This pattern involves creating a 'window' into the data structure and then moving that window around to gather specific information. |
| 20 | +**Usage:** Mostly used in array or list-based problems where you need to find a contiguous subset that fulfills certain conditions. |
| 21 | +**Problems:** 'Maximum Sum Subarray of Size K', 'Smallest Subarray with a given sum', 'Longest Substring with K Distinct Characters'. |
22 | 22 |
|
23 | 23 | ## Pattern: Merge Intervals
|
24 | | -**Description:** This pattern involves merging overlapping intervals. |
25 | | -**Usage:** Often used in problems involving time intervals, ranges, or sequences. |
26 | | -**Problems:** 'Merge Intervals', 'Insert Interval', 'Intervals Intersection'. |
| 24 | +**Description:** This pattern involves merging overlapping intervals. |
| 25 | +**Usage:** Often used in problems involving time intervals, ranges, or sequences. |
| 26 | +**Problems:** 'Merge Intervals', 'Insert Interval', 'Intervals Intersection'. |
27 | 27 |
|
28 | 28 | ## Pattern: Cyclic Sort
|
29 | | -**Description:** This pattern involves sorting an array containing numbers in a given range. |
30 | | -**Usage:** It's useful in situations where the data involves a finite range of natural numbers. |
31 | | -**Problems:** 'Cyclic Sort', 'Find the Missing Number', 'Find all Duplicates'. |
| 29 | +**Description:** This pattern involves sorting an array containing numbers in a given range. |
| 30 | +**Usage:** It's useful in situations where the data involves a finite range of natural numbers. |
| 31 | +**Problems:** 'Cyclic Sort', 'Find the Missing Number', 'Find all Duplicates'. |
32 | 32 |
|
33 | 33 | ## Pattern: In-place Reversal of a Linked List
|
34 | | -**Description:** This pattern involves reversing elements of a linked list in-place. |
35 | | -**Usage:** It's generally used when reversing a sequence without using extra space. |
36 | | -**Problems:** 'Reverse a LinkedList', 'Reverse a Sub-list', 'Reverse Every K-element Sub-list'. |
| 34 | +**Description:** This pattern involves reversing elements of a linked list in-place. |
| 35 | +**Usage:** It's generally used when reversing a sequence without using extra space. |
| 36 | +**Problems:** 'Reverse a LinkedList', 'Reverse a Sub-list', 'Reverse Every K-element Sub-list'. |
37 | 37 |
|
38 | 38 | ## Pattern: Tree Breadth First Search
|
39 | | -**Description:** This pattern involves level-by-level traversal of a tree. |
40 | | -**Usage:** It's used when we need to traverse a tree or graph in a level-by-level (breadth-first) manner. |
41 | | -**Problems:** 'Level Order Traversal', 'Reverse Level Order Traversal', 'Zigzag Traversal'. |
| 39 | +**Description:** This pattern involves level-by-level traversal of a tree. |
| 40 | +**Usage:** It's used when we need to traverse a tree or graph in a level-by-level (breadth-first) manner. |
| 41 | +**Problems:** 'Level Order Traversal', 'Reverse Level Order Traversal', 'Zigzag Traversal'. |
42 | 42 |
|
43 | 43 | ## Pattern: Tree Depth First Search
|
44 | | -**Description:** This pattern involves traversing a tree or graph depth-wise before visiting siblings or neighbors. |
45 | | -**Usage:** It's used when you need to search deeper into a tree/graph first before going across. |
46 | | -**Problems:** 'Binary Tree Path Sum', 'All Paths for a Sum', 'Count Paths for a Sum'. |
| 44 | +**Description:** This pattern involves traversing a tree or graph depth-wise before visiting siblings or neighbors. |
| 45 | +**Usage:** It's used when you need to search deeper into a tree/graph first before going across. |
| 46 | +**Problems:** 'Binary Tree Path Sum', 'All Paths for a Sum', 'Count Paths for a Sum'. |
47 | 47 |
|
48 | 48 | ## Pattern: Two Heaps
|
49 | | -**Description:** This pattern involves using two heaps to divide a set of numbers into two parts. |
50 | | -**Usage:** It's useful when you need to find median numbers in a sequence, or other similar problems. |
51 | | -**Problems:** 'Find the Median of a Number Stream', 'Sliding Window Median', 'Maximize Capital'. |
| 49 | +**Description:** This pattern involves using two heaps to divide a set of numbers into two parts. |
| 50 | +**Usage:** It's useful when you need to find median numbers in a sequence, or other similar problems. |
| 51 | +**Problems:** 'Find the Median of a Number Stream', 'Sliding Window Median', 'Maximize Capital'. |
52 | 52 |
|
53 | 53 | ## Pattern: Subsets
|
54 | | -**Description:** This pattern involves generating all subsets of a set. |
55 | | -**Usage:** It's helpful for solving problems that require exploring all subsets of a given set. |
56 | | -**Problems:** 'Subsets', 'Subsets With Duplicates', 'Permutations'. |
| 54 | +**Description:** This pattern involves generating all subsets of a set. |
| 55 | +**Usage:** It's helpful for solving problems that require exploring all subsets of a given set. |
| 56 | +**Problems:** 'Subsets', 'Subsets With Duplicates', 'Permutations'. |
57 | 57 |
|
58 | 58 | ## Pattern: Modified Binary Search
|
59 | | -**Description:** This is a tweaked version of the binary search algorithm. |
60 | | -**Usage:** It's used when a simple binary search isn't sufficient, like finding a number in a bitonic array. |
61 | | -**Problems:** 'Order-agnostic Binary Search', 'Ceiling of a Number', 'Next Letter'. |
| 59 | +**Description:** This is a tweaked version of the binary search algorithm. |
| 60 | +**Usage:** It's used when a simple binary search isn't sufficient, like finding a number in a bitonic array. |
| 61 | +**Problems:** 'Order-agnostic Binary Search', 'Ceiling of a Number', 'Next Letter'. |
62 | 62 |
|
63 | 63 | ## Pattern: Top 'K' Elements
|
64 | | -**Description:** This pattern is used to find the top 'k' elements among a certain category. |
65 | | -**Usage:** It's commonly used in problems involving sorting, searching, and in heap data structures. |
66 | | -**Problems:** 'Top K Frequent Numbers', 'Kth Largest Number in a Stream', 'Top K Frequent Elements'. |
| 64 | +**Description:** This pattern is used to find the top 'k' elements among a certain category. |
| 65 | +**Usage:** It's commonly used in problems involving sorting, searching, and in heap data structures. |
| 66 | +**Problems:** 'Top K Frequent Numbers', 'Kth Largest Number in a Stream', 'Top K Frequent Elements'. |
67 | 67 |
|
68 | 68 | ## Pattern: Bitwise XOR
|
69 | | -**Description:** This pattern involves the use of Bitwise XOR to solve various array-based problems. |
70 | | -**Usage:** It's used when we need to manipulate and compare bits directly. |
71 | | -**Problems:** 'Single Number', 'Two Single Numbers', 'Complement of Base 10 Number'. |
| 69 | +**Description:** This pattern involves the use of Bitwise XOR to solve various array-based problems. |
| 70 | +**Usage:** It's used when we need to manipulate and compare bits directly. |
| 71 | +**Problems:** 'Single Number', 'Two Single Numbers', 'Complement of Base 10 Number'. |
72 | 72 |
|
73 | 73 | ## Pattern: Backtracking
|
74 | | -**Description:** This pattern involves exploring all possible solutions and then backtracking to correct the course whenever you're on the wrong path. |
75 | | -**Usage:** It's typically used for solving complex combinatorial problems, puzzles, and games. |
76 | | -**Problems:** 'Sudoku Solver', 'N-Queens', 'Generate Parentheses'. |
| 74 | +**Description:** This pattern involves exploring all possible solutions and then backtracking to correct the course whenever you're on the wrong path. |
| 75 | +**Usage:** It's typically used for solving complex combinatorial problems, puzzles, and games. |
| 76 | +**Problems:** 'Sudoku Solver', 'N-Queens', 'Generate Parentheses'. |
77 | 77 |
|
78 | 78 | ## Pattern: 0/1 Knapsack (Dynamic Programming)
|
79 | | -**Description:** This pattern deals with problems where items have different values and weights, and we need to determine the maximum value we can carry. |
80 | | -**Usage:** It's typically used in optimization problems, especially those involving physical constraints. |
81 | | -**Problems:** '0/1 Knapsack', 'Equal Subset Sum Partition', 'Subset Sum'. |
| 79 | +**Description:** This pattern deals with problems where items have different values and weights, and we need to determine the maximum value we can carry. |
| 80 | +**Usage:** It's typically used in optimization problems, especially those involving physical constraints. |
| 81 | +**Problems:** '0/1 Knapsack', 'Equal Subset Sum Partition', 'Subset Sum'. |
82 | 82 |
|
83 | 83 | ## Pattern: Topological Sort (Graph)
|
84 | | -**Description:** This pattern involves sorting nodes in a directed graph in a specific order where the preceding node comes before the following node. |
85 | | -**Usage:** It's used for scheduling problems and in scenarios where order needs to be imposed on how you process nodes. |
86 | | -**Problems:** 'Task Scheduling Order', 'All Tasks Scheduling Orders', 'Alien Dictionary'. |
| 84 | +**Description:** This pattern involves sorting nodes in a directed graph in a specific order where the preceding node comes before the following node. |
| 85 | +**Usage:** It's used for scheduling problems and in scenarios where order needs to be imposed on how you process nodes. |
| 86 | +**Problems:** 'Task Scheduling Order', 'All Tasks Scheduling Orders', 'Alien Dictionary'. |
87 | 87 |
|
88 | 88 | ## Pattern: K-way Merge
|
89 | | -**Description:** This pattern involves merging 'k' sorted lists. |
90 | | -**Usage:** It's typically used in problems involving lists, where merging is required. |
91 | | -**Problems:** 'Merge K Sorted Lists', 'Kth Smallest Number in M Sorted Lists', 'Smallest Number Range'. |
| 89 | +**Description:** This pattern involves merging 'k' sorted lists. |
| 90 | +**Usage:** It's typically used in problems involving lists, where merging is required. |
| 91 | +**Problems:** 'Merge K Sorted Lists', 'Kth Smallest Number in M Sorted Lists', 'Smallest Number Range'. |
92 | 92 |
|
93 | 93 | ## Pattern: Monotonic Stack
|
94 | | -**Description:** This pattern involves using a stack to maintain a monotonic (either entirely non-increasing or non-decreasing) order of elements. |
95 | | -**Usage:** It's often used for solving problems where you need to find the next greater or smaller elements. |
96 | | -**Problems:** 'Next Greater Element', 'Next Smaller Element', 'Largest Rectangle in Histogram'. |
| 94 | +**Description:** This pattern involves using a stack to maintain a monotonic (either entirely non-increasing or non-decreasing) order of elements. |
| 95 | +**Usage:** It's often used for solving problems where you need to find the next greater or smaller elements. |
| 96 | +**Problems:** 'Next Greater Element', 'Next Smaller Element', 'Largest Rectangle in Histogram'. |
97 | 97 |
|
98 | 98 | ## Pattern: Multi-threaded
|
99 | | -**Description:** This pattern involves designing algorithms that can execute multiple threads in parallel. |
100 | | -**Usage:** It's used in situations where a task can be divided into independent sub-tasks that can execute concurrently. |
101 | | -**Problems:** 'Invert Binary Tree', 'Binary Search Tree Iterator', 'Same Tree'. |
| 99 | +**Description:** This pattern involves designing algorithms that can execute multiple threads in parallel. |
| 100 | +**Usage:** It's used in situations where a task can be divided into independent sub-tasks that can execute concurrently. |
| 101 | +**Problems:** 'Invert Binary Tree', 'Binary Search Tree Iterator', 'Same Tree'. |
102 | 102 |
|
103 | 103 | ## Pattern: Union Find
|
104 | | -**Description:** Union Find, also known as Disjoint Set Union (DSU), is a data structure that keeps track of a partition of a set into disjoint subsets. |
105 | | -**Usage:** This pattern is particularly useful for problems where we need to find whether 2 elements belong to the same group or need to solve connectivity-related problems in a graph or tree. |
106 | | -**Problems:** 'Graph Redundant Connection', 'Number of Provinces', 'Is Graph Bipart |
| 104 | +**Description:** Union Find, also known as Disjoint Set Union (DSU), is a data structure that keeps track of a partition of a set into disjoint subsets. |
| 105 | +**Usage:** This pattern is particularly useful for problems where we need to find whether 2 elements belong to the same group or need to solve connectivity-related problems in a graph or tree. |
| 106 | +**Problems:** 'Graph Redundant Connection', 'Number of Provinces', 'Is Graph Bipart'. |
0 commit comments