Hard LeetCode Problems: All 661, With Python Solutions
Hard problems usually combine two techniques, or need a non-obvious invariant before any technique applies at all. They are worth studying for the ideas rather than for the odds of being asked one.
The rating is LeetCode's own, carried through unchanged — we do not re-score anything. This library holds 661 Hard-rated problems, 505 of them with a complete Python solution, a worked example and complexity analysis.
The patterns behind hard problems
The six patterns with the most Hard-rated problems in this library. Working through a pattern beats working down a difficulty list, because the technique transfers and a difficulty band does not.
- Dynamic Programming · 226 Hard problems — Define a state, write the transition, and stop recomputing the same subproblem.
- Math and Number Theory · 120 Hard problems — Find the closed form, the invariant, or the modular identity — and skip the loop entirely.
- Depth-First Search · 111 Hard problems — Follow one path to its end before trying the next — the default way to explore a graph.
- Hash Map · 110 Hard problems — Trade memory for time: remember what you have seen so the second pass never happens.
- Sorting · 94 Hard problems — Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial.
- Monotonic Stack · 89 Hard problems — Answer "what is the next greater element" for every position in one pass.
Hard problems
Showing the first 200 of 661 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 4 | Median of Two Sorted Arrays | Hard | Array, Binary Search, Divide and Conquer |
| 10 | Regular Expression Matching | Hard | Recursion, String, Dynamic Programming |
| 23 | Merge k Sorted Lists | Hard | Linked List, Divide and Conquer, Heap (Priority Queue) +1 |
| 25 | Reverse Nodes in k-Group | Hard | Recursion, Linked List |
| 30 | Substring with Concatenation of All Words | Hard | Hash Table, String, Sliding Window |
| 32 | Longest Valid Parentheses | Hard | Stack, String, Dynamic Programming |
| 37 | Sudoku Solver | Hard | Array, Hash Table, Backtracking +1 |
| 41 | First Missing Positive | Hard | Array, Hash Table |
| 42 | Trapping Rain Water | Hard | Stack, Array, Two Pointers +2 |
| 44 | Wildcard Matching | Hard | Greedy, Recursion, String +1 |
| 51 | N-Queens | Hard | Array, Backtracking |
| 52 | N-Queens II | Hard | Backtracking |
| 60 | Permutation Sequence | Hard | Recursion, Math |
| 65 | Valid Number | Hard | String |
| 68 | Text Justification | Hard | Array, String, Simulation |
| 76 | Minimum Window Substring | Hard | Hash Table, String, Sliding Window |
| 84 | Largest Rectangle in Histogram | Hard | Stack, Array, Monotonic Stack |
| 85 | Maximal Rectangle | Hard | Stack, Array, Dynamic Programming +2 |
| 87 | Scramble String | Hard | String, Dynamic Programming |
| 115 | Distinct Subsequences | Hard | String, Dynamic Programming |
| 123 | Best Time to Buy and Sell Stock III | Hard | Array, Dynamic Programming |
| 124 | Binary Tree Maximum Path Sum | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 126 | Word Ladder II | Hard | Breadth-First Search, Hash Table, String +1 |
| 127 | Word Ladder | Hard | Breadth-First Search, Hash Table, String |
| 132 | Palindrome Partitioning II | Hard | String, Dynamic Programming |
| 135 | Candy | Hard | Greedy, Array |
| 140 | Word Break II | Hard | Trie, Memoization, Array +4 |
| 149 | Max Points on a Line | Hard | Geometry, Array, Hash Table +1 |
| 154 | Find Minimum in Rotated Sorted Array II | Hard | Array, Binary Search |
| 174 | Dungeon Game | Hard | Array, Dynamic Programming, Matrix |
| 185 | Department Top Three Salaries | Hard | Database |
| 188 | Best Time to Buy and Sell Stock IV | Hard | Array, Dynamic Programming |
| 212 | Word Search II | Hard | Trie, Array, String +2 |
| 214 | Shortest Palindrome | Hard | String, String Matching, Hash Function +1 |
| 218 | The Skyline Problem | Hard | Binary Indexed Tree, Segment Tree, Array +5 |
| 220 | Contains Duplicate III | Hard | Array, Bucket Sort, Ordered Set +2 |
| 224 | Basic Calculator | Hard | Stack, Recursion, Math +1 |
| 233 | Number of Digit One | Hard | Recursion, Math, Dynamic Programming |
| 239 | Sliding Window Maximum | Hard | Queue, Array, Sliding Window +2 |
| 262 | Trips and Users | Hard | Database |
| 273 | Integer to English Words | Hard | Recursion, Math, String |
| 282 | Expression Add Operators | Hard | Math, String, Backtracking |
| 295 | Find Median from Data Stream | Hard | Design, Two Pointers, Data Stream +2 |
| 297 | Serialize and Deserialize Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 301 | Remove Invalid Parentheses | Hard | Breadth-First Search, String, Backtracking |
| 312 | Burst Balloons | Hard | Array, Dynamic Programming |
| 315 | Count of Smaller Numbers After Self | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 327 | Count of Range Sum | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 329 | Longest Increasing Path in a Matrix | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 330 | Patching Array | Hard | Greedy, Array |
| 332 | Reconstruct Itinerary | Hard | Depth-First Search, Graph, Eulerian Circuit |
| 335 | Self Crossing | Hard | Geometry, Array, Math |
| 336 | Palindrome Pairs | Hard | Trie, Array, Hash Table +1 |
| 352 | Data Stream as Disjoint Intervals | Hard | Union Find, Design, Hash Table +3 |
| 354 | Russian Doll Envelopes | Hard | Array, Binary Search, Dynamic Programming +1 |
| 363 | Max Sum of Rectangle No Larger Than K | Hard | Array, Binary Search, Matrix +2 |
| 381 | Insert Delete GetRandom O(1) - Duplicates allowed | Hard | Design, Array, Hash Table +2 |
| 391 | Perfect Rectangle | Hard | Geometry, Array, Hash Table +2 |
| 403 | Frog Jump | Hard | Array, Dynamic Programming |
| 407 | Trapping Rain Water II | Hard | Breadth-First Search, Array, Matrix +1 |
| 410 | Split Array Largest Sum | Hard | Greedy, Array, Binary Search +2 |
| 420 | Strong Password Checker | Hard | Greedy, String, Heap (Priority Queue) |
| 432 | All O`one Data Structure | Hard | Design, Hash Table, Linked List +1 |
| 440 | K-th Smallest in Lexicographical Order | Hard | Trie |
| 446 | Arithmetic Slices II - Subsequence | Hard | Array, Dynamic Programming |
| 458 | Poor Pigs | Hard | Math, Dynamic Programming, Combinatorics |
| 460 | LFU Cache | Hard | Design, Hash Table, Linked List +1 |
| 466 | Count The Repetitions | Hard | String, Dynamic Programming |
| 472 | Concatenated Words | Hard | Depth-First Search, Trie, Array +3 |
| 479 | Largest Palindrome Product | Hard | Math, Enumeration |
| 480 | Sliding Window Median | Hard | Array, Hash Table, Sliding Window +1 |
| 483 | Smallest Good Base | Hard | Math, Binary Search |
| 488 | Zuma Game | Hard | Stack, Breadth-First Search, Memoization +2 |
| 493 | Reverse Pairs | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 502 | IPO | Hard | Greedy, Array, Sorting +1 |
| 514 | Freedom Trail | Hard | Depth-First Search, Breadth-First Search, String +1 |
| 517 | Super Washing Machines | Hard | Greedy, Array |
| 546 | Remove Boxes | Hard | Memoization, Array, Dynamic Programming |
| 552 | Student Attendance Record II | Hard | Dynamic Programming |
| 564 | Find the Closest Palindrome | Hard | Math, String |
| 587 | Erect the Fence | Hard | Geometry, Array, Math |
| 591 | Tag Validator | Hard | Stack, String |
| 600 | Non-negative Integers without Consecutive Ones | Hard | Dynamic Programming |
| 601 | Human Traffic of Stadium | Hard | Database |
| 629 | K Inverse Pairs Array | Hard | Dynamic Programming |
| 630 | Course Schedule III | Hard | Greedy, Array, Sorting +1 |
| 632 | Smallest Range Covering Elements from K Lists | Hard | Greedy, Array, Hash Table +3 |
| 639 | Decode Ways II | Hard | String, Dynamic Programming |
| 664 | Strange Printer | Hard | String, Dynamic Programming |
| 668 | Kth Smallest Number in Multiplication Table | Hard | Math, Binary Search |
| 675 | Cut Off Trees for Golf Event | Hard | Breadth-First Search, Array, Matrix +1 |
| 679 | 24 Game | Hard | Array, Math, Backtracking |
| 685 | Redundant Connection II | Hard | Depth-First Search, Breadth-First Search, Union Find +1 |
| 689 | Maximum Sum of 3 Non-Overlapping Subarrays | Hard | Array, Dynamic Programming, Prefix Sum +1 |
| 691 | Stickers to Spell Word | Hard | Bit Manipulation, Memoization, Array +5 |
| 699 | Falling Squares | Hard | Segment Tree, Array, Ordered Set |
| 710 | Random Pick with Blacklist | Hard | Array, Hash Table, Math +3 |
| 715 | Range Module | Hard | Design, Segment Tree, Ordered Set |
| 719 | Find K-th Smallest Pair Distance | Hard | Array, Two Pointers, Binary Search +1 |
| 730 | Count Different Palindromic Subsequences | Hard | String, Dynamic Programming |
| 732 | My Calendar III | Hard | Design, Segment Tree, Binary Search +2 |
| 736 | Parse Lisp Expression | Hard | Stack, Recursion, Hash Table +1 |
| 741 | Cherry Pickup | Hard | Array, Dynamic Programming, Matrix |
| 745 | Prefix and Suffix Search | Hard | Design, Trie, Array +2 |
| 749 | Contain Virus | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 753 | Cracking the Safe | Hard | Depth-First Search, Graph, Eulerian Circuit |
| 757 | Set Intersection Size At Least Two | Hard | Greedy, Array, Sorting |
| 761 | Special Binary String | Hard | Recursion, String |
| 765 | Couples Holding Hands | Hard | Greedy, Depth-First Search, Breadth-First Search +2 |
| 768 | Max Chunks To Make Sorted II | Hard | Stack, Greedy, Array +2 |
| 773 | Sliding Puzzle | Hard | Breadth-First Search, Memoization, Array +3 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 780 | Reaching Points | Hard | Math |
| 782 | Transform to Chessboard | Hard | Bit Manipulation, Array, Math +1 |
| 793 | Preimage Size of Factorial Zeroes Function | Hard | Math, Binary Search |
| 798 | Smallest Rotation with Highest Score | Hard | Array, Prefix Sum |
| 801 | Minimum Swaps To Make Sequences Increasing | Hard | Array, Dynamic Programming |
| 803 | Bricks Falling When Hit | Hard | Union Find, Array, Matrix |
| 805 | Split Array With Same Average | Hard | Bit Manipulation, Array, Math +2 |
| 810 | Chalkboard XOR Game | Hard | Bit Manipulation, Brainteaser, Array +2 |
| 815 | Bus Routes | Hard | Breadth-First Search, Array, Hash Table |
| 818 | Race Car | Hard | Dynamic Programming |
| 827 | Making A Large Island | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 828 | Count Unique Characters of All Substrings of a Given String | Hard | Hash Table, String, Dynamic Programming |
| 829 | Consecutive Numbers Sum | Hard | Math, Enumeration |
| 834 | Sum of Distances in Tree | Hard | Tree, Depth-First Search, Graph +1 |
| 839 | Similar String Groups | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 847 | Shortest Path Visiting All Nodes | Hard | Bit Manipulation, Breadth-First Search, Graph +2 |
| 850 | Rectangle Area II | Hard | Segment Tree, Array, Ordered Set +1 |
| 854 | K-Similar Strings | Hard | Breadth-First Search, Hash Table, String |
| 857 | Minimum Cost to Hire K Workers | Hard | Greedy, Array, Sorting +1 |
| 862 | Shortest Subarray with Sum at Least K | Hard | Queue, Array, Binary Search +4 |
| 864 | Shortest Path to Get All Keys | Hard | Bit Manipulation, Breadth-First Search, Array +1 |
| 871 | Minimum Number of Refueling Stops | Hard | Greedy, Array, Dynamic Programming +1 |
| 878 | Nth Magical Number | Hard | Math, Binary Search |
| 879 | Profitable Schemes | Hard | Array, Dynamic Programming |
| 882 | Reachable Nodes In Subdivided Graph | Hard | Graph, Shortest Path, Heap (Priority Queue) |
| 887 | Super Egg Drop | Hard | Math, Binary Search, Dynamic Programming |
| 891 | Sum of Subsequence Widths | Hard | Array, Math, Sorting |
| 895 | Maximum Frequency Stack | Hard | Stack, Design, Hash Table +1 |
| 899 | Orderly Queue | Hard | Math, String, Sorting |
| 902 | Numbers At Most N Given Digit Set | Hard | Array, Math, String +2 |
| 903 | Valid Permutations for DI Sequence | Hard | String, Dynamic Programming, Prefix Sum |
| 906 | Super Palindromes | Hard | Math, String, Enumeration |
| 913 | Cat and Mouse | Hard | Graph, Topological Sort, Memoization +3 |
| 920 | Number of Music Playlists | Hard | Math, Dynamic Programming, Combinatorics |
| 924 | Minimize Malware Spread | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 927 | Three Equal Parts | Hard | Array, Math |
| 928 | Minimize Malware Spread II | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 936 | Stamping The Sequence | Hard | Stack, Greedy, Queue +1 |
| 940 | Distinct Subsequences II | Hard | String, Dynamic Programming |
| 943 | Find the Shortest Superstring | Hard | Bit Manipulation, Array, String +2 |
| 952 | Largest Component Size by Common Factor | Hard | Union Find, Array, Hash Table +2 |
| 956 | Tallest Billboard | Hard | Array, Dynamic Programming |
| 960 | Delete Columns to Make Sorted III | Hard | Array, String, Dynamic Programming |
| 964 | Least Operators to Express Number | Hard | Memoization, Math, Dynamic Programming |
| 968 | Binary Tree Cameras | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 975 | Odd Even Jump | Hard | Stack, Array, Dynamic Programming +3 |
| 980 | Unique Paths III | Hard | Bit Manipulation, Array, Backtracking +1 |
| 982 | Triples with Bitwise AND Equal To Zero | Hard | Bit Manipulation, Array, Hash Table |
| 987 | Vertical Order Traversal of a Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 992 | Subarrays with K Different Integers | Hard | Array, Hash Table, Counting +1 |
| 995 | Minimum Number of K Consecutive Bit Flips | Hard | Bit Manipulation, Queue, Array +2 |
| 996 | Number of Squareful Arrays | Hard | Bit Manipulation, Array, Hash Table +4 |
| 1000 | Minimum Cost to Merge Stones | Hard | Array, Dynamic Programming, Prefix Sum |
| 1001 | Grid Illumination | Hard | Array, Hash Table |
| 1012 | Numbers With Repeated Digits | Hard | Math, Dynamic Programming |
| 1028 | Recover a Tree From Preorder Traversal | Hard | Tree, Depth-First Search, String +1 |
| 1032 | Stream of Characters | Hard | Design, Trie, Array +2 |
| 1036 | Escape a Large Maze | Hard | Depth-First Search, Breadth-First Search, Array +1 |
| 1044 | Longest Duplicate Substring | Hard | String, Binary Search, Suffix Array +3 |
| 1074 | Number of Submatrices That Sum to Target | Hard | Array, Hash Table, Matrix +1 |
| 1092 | Shortest Common Supersequence | Hard | String, Dynamic Programming |
| 1095 | Find in Mountain Array | Hard | Array, Binary Search, Interactive |
| 1096 | Brace Expansion II | Hard | Stack, Breadth-First Search, Hash Table +3 |
| 1106 | Parsing A Boolean Expression | Hard | Stack, Recursion, String |
| 1125 | Smallest Sufficient Team | Hard | Bit Manipulation, Array, Dynamic Programming +1 |
| 1147 | Longest Chunked Palindrome Decomposition | Hard | Greedy, Two Pointers, String +3 |
| 1157 | Online Majority Element In Subarray | Hard | Design, Binary Indexed Tree, Segment Tree +2 |
| 1163 | Last Substring in Lexicographical Order | Hard | Two Pointers, String |
| 1172 | Dinner Plate Stacks | Hard | Stack, Design, Hash Table +1 |
| 1178 | Number of Valid Words for Each Puzzle | Hard | Bit Manipulation, Trie, Array +2 |
| 1187 | Make Array Strictly Increasing | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1192 | Critical Connections in a Network | Hard | Depth-First Search, Graph, Biconnected Component |
| 1203 | Sort Items by Groups Respecting Dependencies | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
| 1206 | Design Skiplist | Hard | Design, Linked List |
| 1210 | Minimum Moves to Reach Target with Rotations | Hard | Breadth-First Search, Array, Matrix |
| 1220 | Count Vowels Permutation | Hard | Dynamic Programming |
| 1223 | Dice Roll Simulation | Hard | Array, Dynamic Programming |
| 1224 | Maximum Equal Frequency | Hard | Array, Hash Table |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1240 | Tiling a Rectangle with the Fewest Squares | Hard | Backtracking |
| 1250 | Check If It Is a Good Array | Hard | Array, Math, Number Theory |
| 1255 | Maximum Score Words Formed by Letters | Hard | Bit Manipulation, Array, Hash Table +5 |
| 1263 | Minimum Moves to Move a Box to Their Target Location | Hard | Breadth-First Search, Array, Matrix +1 |
| 1269 | Number of Ways to Stay in the Same Place After Some Steps | Hard | Dynamic Programming |
| 1278 | Palindrome Partitioning III | Hard | String, Dynamic Programming |
| 1284 | Minimum Number of Flips to Convert Binary Matrix to Zero Matrix | Hard | Bit Manipulation, Breadth-First Search, Array +2 |
| 1289 | Minimum Falling Path Sum II | Hard | Array, Dynamic Programming, Matrix |
Keep exploring
Everything at once: the full LeetCode problem list.
A hard problem, live, with someone watching
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.