LeetCode Summary
Solutions and thoughts about LeetCode problems.
Data Structrue
Array
- Design Circular Deque
- Set Mismatch
- Pairs of Songs With Total Durations Divisible by 60
- Sum of Digits in the Minimum Number
Linked List
- Plus One Linked List
- Insert into a Sorted Circular Linked List
- Max Stack
- Split Linked List in Parts
- Middle of the Linked List
- Convert Binary Number in a Linked List to Integer
Hash Table
- Isomorphic Strings
- Unique Word Abbreviation
- Word Pattern
- Maximum Size Subarray Sum Equals k
- Design Snake Game
- Line Reflection
- Find Duplicate Subtrees
- Accounts Merge
- Subdomain Visit Count
- Maximum Frequency Stack
- Minimum Area Rectangle
- N-Repeated Element in Size 2N Array
- Subarray Sums Divisible by K
- Cousins in Binary Tree
- Find Common Characters
- Pairs of Songs With Total Durations Divisible by 60
- Longest Arithmetic Sequence
- Longest String Chain
- Unique Number of Occurrences
- Intersection of Three Sorted Arrays
- Longest Arithmetic Subsequence of Given Difference
- Design A Leaderboard
- Find Elements in a Contaminated Binary Tree
- Count Servers that Communicate
- Group the People Given the Group Size They Belong To
- Element Appearing More Than 25% In Sorted Array
- Get Watched Videos by Your Friends
Stack
- Validate Binary Search Tree
- Basic Calculator
- Basic Calculator II
- Inorder Successor in BST (inorder traversal)
- Decode String
- Convert Binary Search Tree to Sorted Doubly Linked List (inorder traversal)
- Tag Validator
- Baseball Game
- Search in a Binary Search Tree
- Asteroid Collision
- Basic Calculator III
- Minimum Distance Between BST Nodes
- Maximum Frequency Stack
- Remove All Adjacent Duplicates In String
- Remove All Adjacent Duplicates in String II
Queue
- Binary Tree Zigzag Level Order Traversal
- Zigzag Iterator
- Walls and Gates
- Design Snake Game
- Design Hit Counter
- Average of Levels in Binary Tree
- Closest Leaf in a Binary Tree
- Open the Lock
- Sliding Puzzle
- Shortest Distance to a Character
- Duplicate Zeros
- Shortest Path in Binary Matrix
- Web Crawler
- Tree Diameter
- Shortest Path in a Grid with Obstacles Elimination
- Deepest Leaves Sum
- Get Watched Videos by Your Friends
Heap
- The Skyline Problem
- Meeting Rooms II
- Course Schedule III (maxHeap)
- Design Search Autocomplete System
- Network Delay Time (shortest path)
- Reorganize String
- Exam Room
- Last Stone Weight (maxHeap)
- Campus Bikes (minHeap)
- Path With Maximum Minimum Value (maxHeap)
- Optimize Water Distribution in a Village (MST)
- Search Suggestions System
- Minimum Falling Path Sum II (minHeap)
- Get Watched Videos by Your Friends
Monotonic Queue
- Largest Rectangle in Histogram
- Sliding Window Maximum
- Daily Temperatures
- Shortest Subarray with Sum at Least K (Deque)
- Online Stock Span
- Sum of Subarray Minimums
Tree
- Validate Binary Search Tree (BST)
- Binary Tree Zigzag Level Order Traversal
- Inorder Successor in BST (BST)
- Binary Tree Vertical Order Traversal (BST)
- Largest BST Subtree (BST)
- Convert Binary Search Tree to Sorted Doubly Linked List (BST)
- Construct String from Binary Tree
- Average of Levels in Binary Tree
- Find Duplicate Subtrees
- Print Binary Tree
- Trim a Binary Search Tree (BST)
- Longest Univalue Path
- Search in a Binary Search Tree (BST)
- Max Stack (TreeMap)
- Closest Leaf in a Binary Tree
- Minimum Distance Between BST Nodes (BST)
- Binary Tree Pruning
- Cousins in Binary Tree
- Delete Nodes And Return Forest
- Design A Leaderboard
- Tree Diameter
- Find Elements in a Contaminated Binary Tree
- Delete Tree Nodes
- All Elements in Two Binary Search Trees (BST)
- Sum of Nodes with Even-Valued Grandparent
Graph
- Alien Dictionary
- Find the Celebrity
- Closest Leaf in a Binary Tree
- Network Delay Time (shortest path)
- Is Graph Bipartite?
- Cheapest Flights Within K Stops
- Optimize Water Distribution in a Village (MST)
- Tree Diameter
- Get Watched Videos by Your Friends
Union Find
- Number of Islands II
- Number of Connected Components in an Undirected Graph (模版题)
- Friend Circles
- Redundant Connection II
- Accounts Merge
- Path With Maximum Minimum Value
- Number of Operations to Make Network Connected
Trie
- Word Search II
- Design Search Autocomplete System
- Search Suggestions System
String
- Longest Substring with At Most Two Distinct Characters
- Largest Number
- Encode and Decode Strings
- Unique Word Abbreviation
- Word Pattern
- Word Pattern II
- Longest Substring with At Most K Distinct Characters
- Robot Return to Origin
- Next Closest Time
- Rotate String
- Goat Latin
- Buddy Strings
- Find Common Characters
- Shortest Way to Form String
- Index Pairs of a String
- Defanging an IP Address
- Remove Sub-Folders from the Filesystem
- Web Crawler
- Hexspeak
- Decrypt String from Alphabet to Integer Mapping
- Minimum Insertion Steps to Make a String Palindrome
Algorithm
Sort
- Largest Number
- Meeting Rooms
- Meeting Rooms II
- Wiggle Sort
- Find the Duplicate Number
- Maximum Product of Three Numbers
- Sort an Array
- Minimum Increment to Make Array Unique
- Pancake Sorting
- Campus Bikes (Bucket Sort)
- Index Pairs of a String
- High Five
- Two Sum Less Than K
- Maximum Profit in Job Scheduling
- Remove Covered Intervals
Two Pointers
- Find the Duplicate Number (fast & slow)
- Plus One Linked List
- Insert into a Sorted Circular Linked List (prev & curr)
- Unique Letter String
- Middle of the Linked List (fast & slow)
- Sort Array By Parity (low & high)
- Two Sum Less Than K (low & high)
- Remove All Adjacent Duplicates in String II (left & right)
- Intersection of Three Sorted Arrays
Binary Search
- Kth Smallest Element in a Sorted Matrix (search in range)
- Random Pick with Weight
- Valid Triangle Number
- Search in a Sorted Array of Unknown Size
- Maximum Length of Repeated Subarray (search in index or length)
- My Calendar I
- My Calendar II
- Koko Eating Bananas (search in range)
- Shortest Way to Form String
- Missing Element in Sorted Array (in index)
- Check If a Number Is Majority Element in a Sorted Array
- Online Majority Element In Subarray
- Intersection of Three Sorted Arrays
- Missing Number In Arithmetic Progression (search in index)
- Maximum Profit in Job Scheduling (search in index)
- Find Positive Integer Solution for a Given Equation
- Element Appearing More Than 25% In Sorted Array (search in index)
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold (search in length)
- Sum of Mutated Array Closest to Target (search in range)
Recursion (DFS)
- Validate Binary Search Tree
- Course Schedule
- Course Schedule II
- Word Search II
- Walls and Gates
- Largest BST Subtree
- Nested List Weight Sum
- Nested List Weight Sum II
- Find Leaves of Binary Tree (post order traversal)
- Plus One Linked List
- Convert Binary Search Tree to Sorted Doubly Linked List (inorder traversal)
- Robot Room Cleaner
- The Maze
- Construct String from Binary Tree
- Find Duplicate Subtrees
- Print Binary Tree
- Trim a Binary Search Tree
- Longest Univalue Path (postorder traversal)
- Employee Importance
- Number of Distinct Islands
- Max Area of Island
- Search in a Binary Search Tree (preorder traversal)
- Candy Crush
- Minimum Distance Between BST Nodes (inorder traversal)
- Is Graph Bipartite?
- Binary Tree Pruning
- Leaf-Similar Trees
- Cousins in Binary Tree
- Sum of Root To Leaf Binary Numbers
- Delete Nodes And Return Forest
- Two Sum BSTs (preorder traversal)
- Queens That Can Attack the King
- Tree Diameter
- Number of Closed Islands
- Find Elements in a Contaminated Binary Tree
- Delete Tree Nodes
- All Elements in Two Binary Search Trees (inorder traversal)
- Sum of Nodes with Even-Valued Grandparent
Backtracking (DFS)
- Generate Parentheses
- Word Ladder II
- Factor Combinations
- Word Pattern II
- Design Search Autocomplete System
- All Paths From Source to Target
- Unique Paths III
- Campus Bikes II
- Brace Expansion
- Stepping Numbers
- Path with Maximum Gold
- Maximum Score Words Formed by Letters
- Iterator for Combination
Recursion with Memoization (Top-down)
- Word Break
- Word Break II
- All Paths From Source to Target
- Campus Bikes II
- Count Vowels Permutation
- Dice Roll Simulation
- Jump Game III
- Minimum Insertion Steps to Make a String Palindrome
Dynamic Programming (Bottom-up)
- Word Break
- House Robber
- Bomb Enemy
- Sentence Screen Fitting
- Valid Parenthesis String
- Best Time to Buy and Sell Stock with Transaction Fee
- Maximum Length of Repeated Subarray
- Delete and Earn
- Minimum Falling Path Sum
- Minimum Cost For Tickets
- Best Sightseeing Pair
- Longest Arithmetic Sequence
- Maximum Sum of Two Non-Overlapping Subarrays
- Longest String Chain
- Longest Arithmetic Subsequence of Given Difference
- Dice Roll Simulation
- Maximum Profit in Job Scheduling
- Maximum Score Words Formed by Letters
- Greatest Sum Divisible by Three
- Minimum Falling Path Sum II
BFS
- Binary Tree Zigzag Level Order Traversal
- Word Ladder II
- Word Ladder
- Course Schedule
- Course Schedule II
- Walls and Gates
- Binary Tree Vertical Order Traversal
- Nested List Weight Sum
- Nested List Weight Sum II
- The Maze
- The Maze II
- Average of Levels in Binary Tree
- Employee Importance
- Closest Leaf in a Binary Tree
- Network Delay Time (shortest path)
- Open the Lock
- Sliding Puzzle
- Is Graph Bipartite?
- All Paths From Source to Target
- Shortest Distance to a Character
- Snakes and Ladders
- Shortest Path in Binary Matrix
- Path With Maximum Minimum Value
- Web Crawler
- Tree Diameter
- Minimum Moves to Move a Box to Their Target Location
- Sequential Digits
- Shortest Path in a Grid with Obstacles Elimination
- Deepest Leaves Sum
- Jump Game III
- Get Watched Videos by Your Friends
Quick Select
- Kth Largest Element in an Array
Morris Traversal
- Validate Binary Search Tree
- Inorder Successor in BST
- Search in a Binary Search Tree
- Minimum Distance Between BST Nodes
Sliding Window
- Minimum Window Substring (模版题)
- Longest Substring with At Most Two Distinct Characters
- Sliding Window Maximum
- Longest Substring with At Most K Distinct Characters
- Replace the Substring for Balanced String
Greedy
- Text Justification
- Find the Celebrity
- Wiggle Sort
- Non-overlapping Intervals
- Minimum Number of Arrows to Burst Balloons
- Can Place Flowers
- Course Schedule III
- Valid Parenthesis String
- Reorganize String
- Shortest Distance to a Character
- Broken Calculator
- Minimum Domino Rotations For Equal Row
- Shortest Way to Form String
- Play with Chips
- Split a String in Balanced Strings
- Reconstruct a 2-Row Binary Matrix
- Group the People Given the Group Size They Belong To
Divide & Conquer
- Count of Smaller Numbers After Self
- Count of Range Sum
Dijkstra's Algorithm (minHeap)
- Network Delay Time
- Cheapest Flights Within K Stops
Topological Sort (DFS, BFS)
- Alien Dictionary
- Course Schedule
- Course Schedule II
Sweep Line
- The Skyline Problem
- Meeting Rooms II
- Range Addition
Bit Manipulation
- Game of Life
- Image Smoother
- Prime Number of Set Bits in Binary Representation
- Campus Bikes II
- Adding Two Negabinary Numbers
- Maximum Score Words Formed by Letters
- XOR Queries of a Subarray
- Minimum Flips to Make a OR b Equal to c
Math
- Basic Calculator II
- Sparse Matrix Multiplication
- Line Reflection
- Plus One Linked List
- Elimination Game
- Maximum Product of Three Numbers
- Basic Calculator III
- Reaching Points
- Rectangle Overlap
- Transpose Matrix
- Minimum Increment to Make Array Unique
- Subarray Sums Divisible by K
- Broken Calculator
- Robot Bounded In Circle
- Confusing Number
- Adding Two Negabinary Numbers
- Sum of Digits in the Minimum Number
- Check If a Number Is Majority Element in a Sorted Array
- Play with Chips
- Check If It Is a Straight Line
- Greatest Sum Divisible by Three
- Minimum Time Visiting All Points
- Hexspeak
- Subtract the Product and Sum of Digits of an Integer
- Convert Binary Number in a Linked List to Integer
Info Cache
- Valid Sudoku
- Design Tic-Tac-Toe
- Find Winner on a Tic Tac Toe Game
Design
- Encode and Decode Strings (Chunked Transfer Encoding)
- Zigzag Iterator
- Design Snake Game
- Design Hit Counter
- Design Circular Deque
- Design HashSet
- Design HashMap
- Maximum Frequency Stack
Intervals
- Meeting Rooms II
- Range Addition
- Non-overlapping Intervals
- Minimum Number of Arrows to Burst Balloons
- My Calendar I
- My Calendar II
- Maximum Profit in Job Scheduling
- Remove Interval
- Remove Covered Intervals
Find Missing/Duplicate Number
- Find the Duplicate Number
- Set Mismatch
Simulation
- Candy Crush
- Pour Water
- Array Transformation
- Cells with Odd Values in a Matrix
- Find Winner on a Tic Tac Toe Game
Prefix Sum
- Maximum Size Subarray Sum Equals k
- Random Pick with Weight
- Find Pivot Index
- Maximum Sum of Two Non-Overlapping Subarrays
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold
- Matrix Block Sum
Synchronization
- Traffic Light Controlled Intersection
Boyer–Moore majority vote algo
- Majority Element
- Majority Element II
- Online Majority Element In Subarray