Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views12 pages

LC Patterns

The document outlines various algorithmic patterns categorized into sections such as Two Pointer Patterns, Sliding Window Patterns, Tree Traversal Patterns, and more. Each pattern includes a list of related problems that exemplify the pattern's application. This structured approach aids in understanding and solving algorithmic challenges efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

LC Patterns

The document outlines various algorithmic patterns categorized into sections such as Two Pointer Patterns, Sliding Window Patterns, Tree Traversal Patterns, and more. Each pattern includes a list of related problems that exemplify the pattern's application. This structured approach aids in understanding and solving algorithmic challenges efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

I.

Two Pointer Patterns


=================================

Pattern 1: Two Pointers - Converging (Sorted Array Target Sum)


---------------------------------
- Two Sum
- Container With Most Water
- 3Sum
- 3Sum Closest
- 4Sum
- Two Sum II - Input Array Is Sorted
- Intersection of Two Arrays
- Boats to Save People
- Squares of a Sorted Array
- 3Sum Smaller

Pattern 2: Two Pointers - Fast & Slow (Cycle Detection)


---------------------------------
- Linked List Cycle
- Happy Number
- Find the Duplicate Number
- Is Subsequence

Pattern 3: Two Pointers - Fixed Separation (Nth Node from End)


---------------------------------
- Remove Nth Node From End of List
- Middle of the Linked List
- Delete the Middle Node of a Linked List

Pattern 4: Two Pointers - In-place Array Modification


---------------------------------
- Remove Duplicates from Sorted Array
- Remove Element
- Sort Colors
- Remove Duplicates from Sorted Array II
- Move Zeroes
- String Compression
- Sort Array By Parity
- Move Pieces to Obtain a String
- Separate Black and White Balls

Pattern 5: Two Pointers - String Comparison with Backspaces


---------------------------------
- Backspace String Compare

Pattern 6: Two Pointers - Expanding From Center (Palindromes)


---------------------------------
- Longest Palindromic Substring
- Palindromic Substrings

Pattern 7: Two Pointers - String Reversal


---------------------------------
- Reverse Words in a String
- Reverse String
- Reverse Vowels of a String
- Reverse String II

II. Sliding Window Patterns


=================================

Pattern 8: Sliding Window - Fixed Size (Subarray Calculation)


---------------------------------
- Moving Average from Data Stream
- Maximum Average Subarray I
- Calculate Compressed Mean
- Find the Power of K-Size Subarrays I
- Find X-Sum of All K-Long Subarrays I

Pattern 9: Sliding Window - Variable Size (Condition-Based)


---------------------------------
- Longest Substring Without Repeating Characters
- Minimum Window Substring
- Minimum Size Subarray Sum
- Contains Duplicate II
- Longest Repeating Character Replacement
- Subarray Product Less Than K
- Fruit Into Baskets
- Max Consecutive Ones III
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
- Longest Subarray of 1's After Deleting One Element
- Minimum Operations to Reduce X to Zero

Pattern 10: Sliding Window - Monotonic Queue for Max/Min


---------------------------------
- Sliding Window Maximum
- Shortest Subarray with Sum at Least K
- Jump Game VI

Pattern 11: Sliding Window - Character Frequency Matching


---------------------------------
- Find All Anagrams in a String
- Permutation in String

III. Tree Traversal Patterns (DFS & BFS)


=================================

Pattern 12: Tree BFS - Level Order Traversal


---------------------------------
- Binary Tree Level Order Traversal
- Binary Tree Zigzag Level Order Traversal
- Binary Tree Right Side View
- Find Largest Value in Each Tree Row
- Maximum Level Sum of a Binary Tree

Pattern 13: Tree DFS - Recursive Preorder Traversal


---------------------------------
- Same Tree
- Symmetric Tree
- Construct Binary Tree from Preorder and Inorder Traversal
- Flatten Binary Tree to Linked List
- Invert Binary Tree
- Binary Tree Paths
- Smallest String Starting From Leaf

Pattern 14: Tree DFS - Recursive Inorder Traversal


---------------------------------
- Binary Tree Inorder Traversal
- Validate Binary Search Tree
- Binary Search Tree Iterator
- Kth Smallest Element in a BST
- Find Mode in Binary Search Tree
- Minimum Absolute Difference in BST

Pattern 15: Tree DFS - Recursive Postorder Traversal


---------------------------------
- Maximum Depth of Binary Tree
- Balanced Binary Tree
- Binary Tree Maximum Path Sum
- Binary Tree Postorder Traversal
- House Robber III
- Find Leaves of Binary Tree
- Diameter of Binary Tree
- All Nodes Distance K in Binary Tree
- Delete Nodes And Return Forest
- Height of Binary Tree After Subtree Removal Queries

Pattern 17: Tree - Lowest Common Ancestor (LCA) Finding


---------------------------------
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Tree

Pattern 18: Tree - Serialization and Deserialization


---------------------------------
- Serialize and Deserialize Binary Tree
- Subtree of Another Tree
- Find Duplicate Subtrees

IV. Graph Traversal Patterns (DFS & BFS)


=================================

Pattern 19: Graph DFS - Connected Components / Island Counting


---------------------------------
- Surrounded Regions
- Number of Islands
- Pacific Atlantic Water Flow
- Number of Provinces
- Max Area of Island
- Flood Fill
- Keys and Rooms
- Number of Enclaves
- Number of Closed Islands
- Count Sub Islands
- Detonate the Maximum Bombs

Pattern 20: Graph BFS - Connected Components / Island Counting


---------------------------------
- Word Ladder
- 01 Matrix
- Rotting Oranges
- Shortest Path in Binary Matrix

Pattern 21: Graph DFS - Cycle Detection (Directed Graph)


---------------------------------
- Course Schedule
- Course Schedule II
- Find Eventual Safe States
- All Paths from Source Lead to Destination

Pattern 22: Graph BFS - Topological Sort (Kahn's Algorithm)


---------------------------------
- Course Schedule
- Course Schedule II
- Alien Dictionary
- Minimum Height Trees
- Sequence Reconstruction
- Parallel Courses
- Largest Color Value in a Directed Graph
- Parallel Courses III
- Find All Possible Recipes from Given Supplies
- Build a Matrix With Conditions

Pattern 23: Graph - Deep Copy / Cloning


---------------------------------
- Clone Graph

Pattern 24: Graph - Shortest Path (Dijkstra's Algorithm)


---------------------------------
- Network Delay Time
- Swim in Rising Water
- Path with Maximum Probability
- Path With Minimum Effort
- Number of Ways to Arrive at Destination
- Second Minimum Time to Reach Destination
- Minimum Weighted Subgraph With the Required Paths
- Minimum Obstacle Removal to Reach Corner
- Minimum Time to Visit a Cell In a Grid
- Find the Safest Path in a Grid

Pattern 25: Graph - Shortest Path (Bellman-Ford / BFS+K)


---------------------------------
- Cheapest Flights Within K Stops

Pattern 26: Graph - Union-Find (Disjoint Set Union - DSU)


---------------------------------
- Number of Islands
- Graph Valid Tree
- Number of Islands II
- Number of Connected Components in an Undirected Graph
- Number of Provinces
- Redundant Connection
- Accounts Merge
- Sentence Similarity II
- Most Stones Removed with Same Row or Column
- Largest Component Size by Common Factor
- Regions Cut By Slashes
- The Earliest Moment When Everyone Become Friends

V. Dynamic Programming (DP) Patterns


=================================

Pattern 27: DP - 1D Array (Fibonacci Style)


---------------------------------
- Climbing Stairs
- Decode Ways
- House Robber
- House Robber II
- House Robber III
- Fibonacci Number
- Delete and Earn
- Min Cost Climbing Stairs

Pattern 28: DP - 1D Array (Kadane's Algorithm for Max/Min Subarray)


---------------------------------
- Maximum Subarray

Pattern 29: DP - 1D Array (Coin Change / Unbounded Knapsack Style)


---------------------------------
- Coin Change
- Combination Sum IV
- Coin Change II

Pattern 30: DP - 1D Array (0/1 Knapsack Subset Sum Style)


---------------------------------
- Partition Equal Subset Sum
- Target Sum

Pattern 31: DP - 1D Array (Word Break Style)


---------------------------------
- Word Break
- Word Break II

Pattern 32: DP - 2D Array (Longest Common Subsequence - LCS)


---------------------------------
- Delete Operation for Two Strings
- Longest Common Subsequence

Pattern 33: DP - 2D Array (Edit Distance / Levenshtein Distance)


---------------------------------
- Edit Distance

Pattern 34: DP - 2D Array (Unique Paths on Grid)


---------------------------------
- Unique Paths
- Unique Paths II
- Minimum Path Sum
- Triangle
- Maximal Square
- Minimum Falling Path Sum
- Count Square Submatrices with All Ones

Pattern 35: DP - Interval DP


---------------------------------
- Burst Balloons
- Remove Boxes

Pattern 36: DP - Catalan Numbers


---------------------------------
- Unique Binary Search Trees II
- Unique Binary Search Trees
- Different Ways to Add Parentheses
Pattern 37: DP - Longest Increasing Subsequence (LIS)
---------------------------------
- Longest Increasing Subsequence
- Russian Doll Envelopes
- Minimum Number of Removals to Make Mountain Array
- Longest Increasing Subsequence II

VI. Heap (Priority Queue) Patterns


=================================

Pattern 38: Heap - Top K Elements (Selection/Frequency)


---------------------------------
- Kth Largest Element in an Array
- Top K Frequent Elements
- Sort Characters By Frequency
- Relative Ranks
- Kth Largest Element in a Stream
- K Closest Points to Origin
- Last Stone Weight
- Take Gifts From the Richest Pile

Pattern 39: Heap - Two Heaps for Median Finding


---------------------------------
- Find Median from Data Stream
- Finding MK Average

Pattern 40: Heap - K-way Merge


---------------------------------
- Merge k Sorted Lists
- Find K Pairs with Smallest Sums
- Kth Smallest Element in a Sorted Matrix
- Smallest Range Covering Elements from K Lists

Pattern 41: Heap - Scheduling / Minimum Cost (Greedy with Priority Queue)
---------------------------------
- Meeting Rooms II
- Reorganize String
- Minimum Cost to Hire K Workers
- Furthest Building You Can Reach
- Maximum Average Pass Ratio
- Single-Threaded CPU
- The Number of the Smallest Unoccupied Chair
- Meeting Rooms III

VII. Backtracking Patterns


=================================

Pattern 42: Backtracking - Subsets (Include/Exclude)


---------------------------------
- Letter Combinations of a Phone Number
- Combinations
- Subsets
- Subsets II

Pattern 43: Backtracking - Permutations


---------------------------------
- Next Permutation
- Permutations
- Permutation Sequence

Pattern 44: Backtracking - Combination Sum


---------------------------------
- Combination Sum
- Combination Sum II

Pattern 45: Backtracking - Parentheses Generation


---------------------------------
- Generate Parentheses
- Remove Invalid Parentheses

Pattern 46: Backtracking - Word Search / Path Finding in Grid


---------------------------------
- Word Search
- Word Search II
- Check if Word Can be Placed In Crossword

Pattern 47: Backtracking - N-Queens / Constraint Satisfaction


---------------------------------
- Sudoku Solver
- N-Queens

Pattern 48: Backtracking - Palindrome Partitioning


---------------------------------
- Palindrome Partitioning

VIII. Greedy Patterns


=================================

Pattern 49: Greedy - Interval Merging/Scheduling


---------------------------------
- Merge Intervals
- Insert Interval
- Employee Free Time
- Interval List Intersections
- Divide Intervals Into Minimum Number of Groups

Pattern 51: Greedy - Jump Game Reachability/Minimization


---------------------------------
- Jump Game II
- Jump Game

Pattern 52: Greedy - Buy/Sell Stock


---------------------------------
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II

Pattern 53: Greedy - Gas Station Circuit


---------------------------------
- Gas Station

Pattern 54: Greedy - Task Scheduling (Frequency Based)


---------------------------------
- Task Scheduler
IX. Binary Search Patterns
=================================

Pattern 55: Binary Search - On Sorted Array/List


---------------------------------
- Search Insert Position
- Sqrt(x)
- Search a 2D Matrix
- First Bad Version
- Guess Number Higher or Lower
- Single Element in a Sorted Array
- Binary Search
- Kth Missing Positive Number

Pattern 56: Binary Search - Find Min/Max in Rotated Sorted Array


---------------------------------
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array II
- Find Minimum in Rotated Sorted Array
- Find Peak Element
- Peak Index in a Mountain Array
- Find in Mountain Array

Pattern 57: Binary Search - On Answer / Condition Function


---------------------------------
- Split Array Largest Sum
- Minimize Max Distance to Gas Station
- Koko Eating Bananas
- Capacity To Ship Packages Within D Days
- Minimum Number of Days to Make m Bouquets
- Minimum Limit of Balls in a Bag
- Minimized Maximum of Products Distributed to Any Store
- Maximum Candies Allocated to K Children

Pattern 58: Binary Search - Find First/Last Occurrence


---------------------------------
- Find First and Last Position of Element in Sorted Array
- Find K Closest Elements

Pattern 59: Binary Search - Median of Two Sorted Arrays


---------------------------------
- Median of Two Sorted Arrays

X. Stack Patterns
=================================

Pattern 60: Stack - Valid Parentheses Matching


---------------------------------
- Valid Parentheses
- Longest Valid Parentheses
- Minimum Add to Make Parentheses Valid
- Minimum Remove to Make Valid Parentheses
- Minimum Number of Swaps to Make the String Balanced

Pattern 61: Stack - Monotonic Stack


---------------------------------
- Remove K Digits
- Next Greater Element I
- Next Greater Element II
- Daily Temperatures
- Online Stock Span
- Sum of Subarray Minimums
- Maximum Width Ramp
- Final Prices With a Special Discount in a Shop
- Find the Most Competitive Subsequence

Pattern 62: Stack - Expression Evaluation (RPN/Infix)


---------------------------------
- Evaluate Reverse Polish Notation
- Basic Calculator
- Basic Calculator II
- Basic Calculator III

Pattern 63: Stack - Simulation / Backtracking Helper


---------------------------------
- Simplify Path
- Decode String
- Asteroid Collision

Pattern 64: Stack - Min Stack Design


---------------------------------
- Min Stack

Pattern 65: Stack - Largest Rectangle in Histogram


---------------------------------
- Largest Rectangle in Histogram
- Maximal Rectangle

XI. Bit Manipulation Patterns


=================================

Pattern 66: Bitwise XOR - Finding Single/Missing Number


---------------------------------
- Single Number
- Single Number II
- Missing Number
- Find the Difference

Pattern 67: Bitwise AND - Counting Set Bits (Hamming Weight)


---------------------------------
- Number of 1 Bits

Pattern 70: Bitwise DP - Counting Bits Optimization


---------------------------------
- Counting Bits

Pattern 69: Bitwise Operations - Power of Two/Four Check


---------------------------------
- Power of Two
- Power of Four

XII. Linked List Manipulation Patterns


=================================

Pattern 71: Linked List - In-place Reversal


---------------------------------
- Remove Duplicates from Sorted List
- Reverse Linked List II
- Reverse Linked List
- Reverse Nodes in k-Group
- Palindrome Linked List
- Remove Duplicates from Sorted List II

Pattern 72: Linked List - Merging Two Sorted Lists


---------------------------------
- Merge Two Sorted Lists

Pattern 73: Linked List - Addition of Numbers


---------------------------------
- Add Two Numbers
- Plus One Linked List

Pattern 74: Linked List - Intersection Detection


---------------------------------
- Intersection of Two Linked Lists

Pattern 75: Linked List - Reordering / Partitioning


---------------------------------
- Swap Nodes in Pairs
- Rotate List
- Partition List
- Reorder List
- Odd Even Linked List

XIII. Array/Matrix Manipulation Patterns


=================================

Pattern 76: Array/Matrix - In-place Rotation


---------------------------------
- Rotate Image
- Rotate Array

Pattern 77: Array/Matrix - Spiral Traversal


---------------------------------
- Spiral Matrix
- Spiral Matrix III
- Spiral Matrix IV

Pattern 78: Array/Matrix - Set Matrix Zeroes (In-place Marking)


---------------------------------
- Set Matrix Zeroes

Pattern 79: Array - Product Except Self (Prefix/Suffix Products)


---------------------------------
- Product of Array Except Self

Pattern 80: Array - Plus One (Handling Carry)


---------------------------------
- Plus One

Pattern 81: Array - Merge Sorted Array (In-place from End)


---------------------------------
- Merge Sorted Array
Pattern 82: Array - Cyclic Sort
---------------------------------
- First Missing Positive
- Missing Number
- Find the Duplicate Number
- Find All Duplicates in an Array
- Find All Numbers Disappeared in an Array

Pattern 83: Array - Kadane's Variant for Maximum Product


---------------------------------
- Maximum Product Subarray

XIV. String Manipulation Patterns


=================================

Pattern 84: String - Palindrome Check (Two Pointers / Reverse)


---------------------------------
- Palindrome Number
- Valid Palindrome
- Valid Palindrome II

Pattern 85: String - Anagram Check (Frequency Count/Sort)


---------------------------------
- Group Anagrams
- Valid Anagram

Pattern 86: String - Roman to Integer Conversion


---------------------------------
- Roman to Integer

Pattern 87: String - String to Integer (atoi)


---------------------------------
- String to Integer (atoi)

Pattern 88: String - Multiply Strings (Manual Simulation)


---------------------------------
- Multiply Strings

Pattern 89: String Matching - Naive / KMP / Rabin-Karp


---------------------------------
- Find the Index of the First Occurrence in a String
- Shortest Palindrome
- Repeated String Match
- Rotate String
- Find Beautiful Indices in the Given Array II

Pattern 90: String - Repeated Substring Pattern Detection


---------------------------------
- Repeated Substring Pattern

XV. Design (General/Specific)


=================================
- LRU Cache
- Min Stack
- Implement Trie (Prefix Tree)
- Design Add and Search Words Data Structure
- Implement Stack using Queues
- Implement Queue using Stacks
- Flatten 2D Vector
- Encode and Decode Strings
- Find Median from Data Stream
- Flatten Nested List Iterator
- Moving Average from Data Stream
- Design Snake Game
- Logger Rate Limiter
- Design Hit Counter

You might also like