100 most asked LeetCode Problem Solving Patterns

LeetCode patterns refer to common techniques or strategies used to solve coding problems on platforms like LeetCode. Recognizing and mastering these patterns can help you tackle a wide range of algorithmic challenges efficiently. Here are some key LeetCode patterns:

  1. Two Pointers:
    • Use Case: Problems involving arrays, linked lists, or strings where you need to compare elements or find a pair.
    • Example Problems: Find the pair with a given sum, remove duplicates from a sorted array.
  2. Sliding Window:
    • Use Case: Problems that involve finding subarrays or substrings within a given window size.
    • Example Problems: Maximum sum subarray of size k, longest substring without repeating characters.
  3. Fast and Slow Pointers:
    • Use Case: Problems involving cycles in linked lists or arrays.
    • Example Problems: Detect cycle in a linked list, find the middle of a linked list.
  4. Merge Intervals:
    • Use Case: Problems involving overlapping intervals or intervals management.
    • Example Problems: Merge overlapping intervals, insert interval.
  5. Cyclic Sort:
    • Use Case: Problems where numbers are in a given range, typically 1 to n.
    • Example Problems: Find the missing number, find all duplicates in an array.
  6. In-place Reversal of a LinkedList:
    • Use Case: Problems that require reversing a part or whole of a linked list.
    • Example Problems: Reverse a linked list, reverse nodes in k-group.
  7. Tree Depth First Search (DFS):
    • Use Case: Problems that involve traversing a tree or graph deeply.
    • Example Problems: Path sum, all paths for a sum.
  8. Tree Breadth First Search (BFS):
    • Use Case: Problems that require level order traversal of a tree or graph.
    • Example Problems: Level order traversal, minimum depth of a binary tree.
  9. Binary Search:
    • Use Case: Problems that involve searching in a sorted array or finding an element efficiently.
    • Example Problems: Find an element in a rotated array, search in a sorted array.
  10. Topological Sort:
    • Use Case: Problems involving ordering tasks or nodes based on dependencies.
    • Example Problems: Course schedule, alien dictionary.
  11. Dynamic Programming (DP):
    • Use Case: Problems involving optimization and overlapping subproblems.
    • Example Problems: Fibonacci sequence, knapsack problem, longest common subsequence.
  12. Backtracking:
    • Use Case: Problems that involve exploring all potential solutions and choosing the optimal one.
    • Example Problems: N-Queens, Sudoku solver, permutations.
  13. Union Find:
    • Use Case: Problems involving connected components in a graph.
    • Example Problems: Number of connected components, detect cycle in an undirected graph.
  14. Bit Manipulation:
    • Use Case: Problems that involve manipulating bits to achieve the solution.
    • Example Problems: Single number, count the number of 1 bits.
  1. Matrix Traversal:
    • Use Case: Problems involving navigating or processing a 2D grid or matrix.
    • Example Problems: Number of islands, search a 2D matrix.
  2. Greedy Algorithms:
    • Use Case: Problems where local optimal choices lead to a global optimal solution.
    • Example Problems: Interval scheduling, coin change problem, largest sum of non-adjacent numbers.
  3. Heap/Priority Queue:
    • Use Case: Problems requiring frequent access to the smallest or largest element.
    • Example Problems: Kth largest element in an array, merge k sorted lists.
  4. Prefix Sum:
    • Use Case: Problems involving cumulative sums or differences within subarrays.
    • Example Problems: Subarray sum equals k, range sum query.
  5. Counting and Hashing:
    • Use Case: Problems involving frequency counts or hash maps for quick lookups.
    • Example Problems: Two sum, group anagrams.
  6. Palindromic Substrings:
    • Use Case: Problems involving identifying or counting palindromic substrings or subsequences.
    • Example Problems: Longest palindromic substring, palindromic substrings count.
  7. Graph Traversal:
    • Use Case: Problems involving traversal of graphs using DFS or BFS.
    • Example Problems: Clone graph, shortest path in a binary matrix.
  8. Segment Tree:
    • Use Case: Problems involving range queries and updates.
    • Example Problems: Range sum query, range minimum query.
  9. Trie:
    • Use Case: Problems involving efficient retrieval of words or prefixes from a dictionary.
    • Example Problems: Word search II, implement a trie (prefix tree).
  10. Reservoir Sampling:
    • Use Case: Problems that involve sampling random elements from a stream of unknown size.
    • Example Problems: Random pick index, linked list random node.
  11. Monotonic Stack/Queue:
    • Use Case: Problems where you need to maintain elements in a sorted order dynamically.
    • Example Problems: Next greater element, sliding window maximum.
  12. Knapsack Pattern:
    • Use Case: Problems that involve selection of items with constraints to maximize or minimize a value.
    • Example Problems: 0/1 knapsack, partition equal subset sum.
  13. Combinatorial Search:
    • Use Case: Problems that require generating combinations or permutations of elements.
    • Example Problems: Combination sum, subsets, permutations.
  14. Disjoint Set (Union-Find):
    • Use Case: Problems involving union and find operations to manage a collection of disjoint sets.
    • Example Problems: Friend circles, number of connected components in an undirected graph.
  15. Cycle Detection in Graphs:
    • Use Case: Problems that require detecting cycles in directed or undirected graphs.
    • Example Problems: Course schedule II, detect cycle in a directed graph.
  16. Flood Fill Algorithm:
    • Use Case: Problems involving region-based operations in matrices.
    • Example Problems: Flood fill, number of enclaves.
  17. Binary Indexed Tree (Fenwick Tree):
    • Use Case: Problems that involve efficient updates and queries on prefix sums.
    • Example Problems: Range sum query, range sum and update.
  18. Divide and Conquer:
    • Use Case: Problems that can be divided into smaller subproblems, solved independently, and combined.
    • Example Problems: Merge sort, quicksort, closest pair of points.
  19. Reservoir Sampling:
    • Use Case: Problems involving sampling k elements from a stream.
    • Example Problems: Random pick index, linked list random node.
See also  Understanding the Bin Packing Problem on LeetCode
  1. Binary Search Tree (BST) Operations:
    • Use Case: Problems involving operations on BSTs like insertion, deletion, and traversal.
    • Example Problems: Validate BST, kth smallest element in a BST.
  2. Bitmasking:
    • Use Case: Problems where you need to represent and manipulate subsets using bits.
    • Example Problems: Subset sum, maximum product of word lengths.
  3. Dynamic Programming on Trees:
    • Use Case: DP problems applied to tree structures, often using post-order traversal.
    • Example Problems: House robber III, longest path in a tree.
  4. Matrix Exponentiation:
    • Use Case: Problems involving efficient computation of large powers of matrices.
    • Example Problems: Fibonacci sequence in logarithmic time.
  5. Meet in the Middle:
    • Use Case: Problems that benefit from splitting into two halves and solving each half independently.
    • Example Problems: Subset sum problem for large sets.
  6. Line Sweep Algorithm:
    • Use Case: Problems involving processing events in a sorted order.
    • Example Problems: Skyline problem, meeting rooms II.
  7. Eulerian Path and Circuit:
    • Use Case: Problems involving finding paths in graphs that visit every edge exactly once.
    • Example Problems: Reconstruct itinerary, valid arrangement of pairs.
  8. Bellman-Ford Algorithm:
    • Use Case: Shortest path problems that handle negative weights.
    • Example Problems: Shortest path in a graph with negative weights.
  9. Floyd-Warshall Algorithm:
    • Use Case: Finding shortest paths between all pairs of vertices in a graph.
    • Example Problems: All-pairs shortest path problem.
  10. Tarjan’s Algorithm:
    • Use Case: Finding strongly connected components in a graph.
    • Example Problems: Critical connections in a network.
  11. Hopcroft-Karp Algorithm:
    • Use Case: Maximum bipartite matching problems.
    • Example Problems: Maximum number of matching in bipartite graph.
  12. Shortest Path Faster Algorithm (SPFA):
    • Use Case: Finding shortest paths in graphs, an optimization of Bellman-Ford.
    • Example Problems: Shortest path in a graph with negative weights.
  13. Dynamic Connectivity:
    • Use Case: Problems involving connectivity queries in dynamic graphs.
    • Example Problems: Dynamic connectivity problem.
  14. Subset Sum with Memoization:
    • Use Case: Problems involving checking possible subsets that meet certain conditions.
    • Example Problems: Partition equal subset sum, target sum.
  15. Catalan Numbers:
    • Use Case: Problems that count certain types of combinatorial structures.
    • Example Problems: Number of unique BSTs, valid parentheses.
  16. Manacher’s Algorithm:
    • Use Case: Finding the longest palindromic substring in linear time.
    • Example Problems: Longest palindromic substring.
  17. Cycle Sort:
    • Use Case: Finding duplicates in arrays where elements are in a known range.
    • Example Problems: Find the duplicate number.
  18. Bitwise XOR for Inverse Operations:
    • Use Case: Problems involving toggling and finding unique elements.
    • Example Problems: Single number, two single numbers.
  19. Sparse Table:
    • Use Case: Range query problems that require efficient querying with static data.
    • Example Problems: Range minimum query, range maximum query.
  20. Suffix Array and Suffix Tree:
    • Use Case: String processing problems involving efficient substring search.
    • Example Problems: Longest common substring, repeated DNA sequences.
  21. Segment Trees with Lazy Propagation:
    • Use Case: Problems involving range updates and queries.
    • Example Problems: Range sum query with updates, range minimum query with updates.
  22. Heavy-Light Decomposition:
    • Use Case: Efficiently answering queries on paths in trees.
    • Example Problems: Path queries in trees.
  23. Mo’s Algorithm:
    • Use Case: Answering offline range queries efficiently.
    • Example Problems: Number of distinct elements in a range, range sum queries.
  24. KMP Algorithm:
    • Use Case: String matching problems.
    • Example Problems: Pattern search in a string.
  25. Aho-Corasick Algorithm:
    • Use Case: Multiple pattern matching problems.
    • Example Problems: Search multiple patterns in a text.
  26. Z-Algorithm:
    • Use Case: Pattern matching and string preprocessing.
    • Example Problems: Find all occurrences of a pattern in a text.
  27. Extended Euclidean Algorithm:
    • Use Case: Problems involving finding modular inverses.
    • Example Problems: Solve linear Diophantine equations.
See also  Randomized algorithms - The basics of using Randomness for problem solving
  1. Minimum Spanning Tree (MST):
    • Use Case: Problems involving connecting all points in a graph with the minimum total edge weight.
    • Example Problems: Kruskal’s algorithm, Prim’s algorithm.
  2. Articulation Points and Bridges:
    • Use Case: Finding critical nodes and edges in a graph whose removal would increase the number of connected components.
    • Example Problems: Critical connections in a network, articulation points in a graph.
  3. Network Flow:
    • Use Case: Problems involving maximum flow in networks.
    • Example Problems: Ford-Fulkerson algorithm, Edmonds-Karp algorithm, maximum bipartite matching.
  4. Convex Hull:
    • Use Case: Finding the smallest convex polygon that can enclose a set of points.
    • Example Problems: Graham’s scan, Jarvis’s march.
  5. Graph Coloring:
    • Use Case: Problems where you need to color vertices of a graph so that no two adjacent vertices share the same color.
    • Example Problems: Map coloring, scheduling problems.
  6. Knuth-Morris-Pratt (KMP) Algorithm:
    • Use Case: Efficient pattern matching in strings.
    • Example Problems: Substring search, finding patterns in text.
  7. Union by Rank and Path Compression:
    • Use Case: Optimizing the union-find algorithm.
    • Example Problems: Dynamic connectivity, Kruskal’s MST algorithm.
  8. Hamiltonian Path and Circuit:
    • Use Case: Problems involving finding a path or circuit that visits each vertex exactly once.
    • Example Problems: Traveling salesman problem.
  9. Counting Inversions:
    • Use Case: Counting the number of inversions in an array.
    • Example Problems: Using merge sort to count inversions.
  10. Randomized Algorithms:
    • Use Case: Algorithms that use a degree of randomness to solve problems.
    • Example Problems: Randomized quicksort, Monte Carlo methods.
  11. Divide and Conquer for Convolution:
    • Use Case: Efficiently multiplying large numbers or polynomials.
    • Example Problems: Karatsuba algorithm, fast Fourier transform (FFT).
  12. Exponentiation by Squaring:
    • Use Case: Efficiently computing large powers.
    • Example Problems: Modular exponentiation, power of a number.
  13. Burnside’s Lemma:
    • Use Case: Counting distinct objects under symmetry.
    • Example Problems: Counting distinct colorings of a graph.
  14. Inclusion-Exclusion Principle:
    • Use Case: Problems involving counting the number of elements in the union of several sets.
    • Example Problems: Counting intersections of sets, derangements.
  15. Catalan Numbers and Combinatorial Counting:
    • Use Case: Counting specific types of combinatorial structures.
    • Example Problems: Number of valid parentheses expressions, number of BSTs.
  16. Fenwick Tree (Binary Indexed Tree):
    • Use Case: Efficiently update elements and calculate prefix sums in a table of numbers.
    • Example Problems: Range sum queries, dynamic prefix sums.
  17. Red-Black Trees and AVL Trees:
    • Use Case: Self-balancing binary search trees.
    • Example Problems: Maintaining a sorted list with insertions and deletions.
  18. Treap (Tree + Heap):
    • Use Case: A randomized binary search tree with heap properties.
    • Example Problems: Balanced binary search operations, order statistics.
  19. Suffix Automaton:
    • Use Case: Solving problems related to substrings of a string efficiently.
    • Example Problems: Longest common substring, number of distinct substrings.
  20. Planar Graph Algorithms:
    • Use Case: Algorithms specifically designed for planar graphs.
    • Example Problems: Planarity testing, face traversal.
  21. Cycle Detection in Directed Graphs:
    • Use Case: Detecting cycles in a directed graph.
    • Example Problems: Course scheduling, dependency resolution.
  22. 2-SAT Problem:
    • Use Case: Solving certain types of boolean satisfiability problems.
    • Example Problems: Implication graphs, variable assignments.
  23. Link/Cut Trees:
    • Use Case: Dynamic tree data structure.
    • Example Problems: Dynamic connectivity, dynamic tree queries.
  24. Persistent Data Structures:
    • Use Case: Data structures that preserve the previous version of the data when it is modified.
    • Example Problems: Versioned array, persistent segment trees.
  25. Range Minimum Query (RMQ):
    • Use Case: Finding the minimum value in a subarray.
    • Example Problems: Static RMQ, dynamic RMQ.
  26. Heavy-Light Decomposition (HLD):
    • Use Case: Answering path queries efficiently in trees.
    • Example Problems: Path sum queries, path maximum queries.
  27. Dynamic Programming on Graphs:
    • Use Case: DP problems extended to graphs.
    • Example Problems: Shortest path in DAG, longest path in DAG.
  28. Convex Hull Trick:
    • Use Case: Optimizing dynamic programming problems with linear functions.
    • Example Problems: Minimum cost paths, dynamic range queries.
  29. Sqrt Decomposition:
    • Use Case: Breaking down a problem into manageable blocks for efficient querying and updating.
    • Example Problems: Range sum queries, range minimum queries.
  30. Heavy Path Decomposition:
    • Use Case: Decomposing trees for efficient query operations.
    • Example Problems: Path queries in trees, subtree queries.
  31. Online Algorithms:
    • Use Case: Algorithms that process input piece-by-piece in a serial fashion.
    • Example Problems: Online caching, competitive algorithms.
  32. Quantum Algorithms:
    • Use Case: Leveraging principles of quantum mechanics for problem-solving.
    • Example Problems: Shor’s algorithm for factoring, Grover’s search algorithm.
  33. De Bruijn Sequences:
    • Use Case: Sequences that represent all possible sequences of a given length.
    • Example Problems: Sequence reconstruction, generating all binary strings.
  34. Automata Theory:
    • Use Case: Designing and understanding computational models like finite state machines.
    • Example Problems: Regular expression matching, parsing sequences.
  35. Probabilistic Data Structures:
    • Use Case: Using probabilistic techniques to handle large data efficiently.
    • Example Problems: Bloom filters, Count-Min sketch.
See also  Understanding the Hamiltonian Path Problem on LeetCode
  1. Randomized Select:
    • Use Case: Finding the k-th smallest/largest element in an unordered list.
    • Example Problems: Quickselect algorithm.
  2. Game Theory:
    • Use Case: Analyzing competitive situations to determine optimal strategies.
    • Example Problems: Nim game, tic-tac-toe.
  3. Approximation Algorithms:
    • Use Case: Finding near-optimal solutions to optimization problems.
    • Example Problems: Traveling salesman problem, vertex cover.
  4. Branch and Bound:
    • Use Case: Solving combinatorial and optimization problems by systematically exploring candidate solutions.
    • Example Problems: 0/1 knapsack problem, traveling salesman problem.
  5. Polynomial Time Approximation Scheme (PTAS):Use Case: Finding solutions within a factor of (1 + ε) of the optimal solution in polynomial time. – Example Problems: Euclidean traveling salesman problem, bin packing problem.

These 100 patterns and techniques cover a broad range of problem-solving strategies, each with its own set of applications and example problems. Mastery of these patterns can significantly enhance your ability to tackle a wide variety of coding challenges and algorithmic problems.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get a Quote

Give us a call or fill in the form below and we will contact you. We endeavor to answer all inquiries within 24 hours on business days.