Sorting LeetCode Problems: All 392, With Python Solutions
Every problem in this library that LeetCode tags Sorting — 392 in total, 339 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 392 problems
- 80 Easy
- 226 Medium
- 86 Hard
How Sorting problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Sorting problems — each one explains the approach, gives a Python template and states its complexity.
- Sorting — Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial.
Sorting problems by difficulty
Showing the first 200 of 392 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (43)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 88 | Merge Sorted Array | Easy | Array, Two Pointers, Sorting |
| 169 | Majority Element | Easy | Array, Hash Table, Divide and Conquer +2 |
| 217 | Contains Duplicate | Easy | Array, Hash Table, Sorting |
| 242 | Valid Anagram | Easy | Hash Table, String, Sorting |
| 268 | Missing Number | Easy | Bit Manipulation, Array, Hash Table +3 |
| 349 | Intersection of Two Arrays | Easy | Array, Hash Table, Two Pointers +2 |
| 350 | Intersection of Two Arrays II | Easy | Array, Hash Table, Two Pointers +2 |
| 389 | Find the Difference | Easy | Bit Manipulation, Hash Table, String +1 |
| 414 | Third Maximum Number | Easy | Array, Sorting |
| 455 | Assign Cookies | Easy | Greedy, Array, Two Pointers +1 |
| 506 | Relative Ranks | Easy | Array, Sorting, Heap (Priority Queue) |
| 561 | Array Partition | Easy | Greedy, Array, Counting Sort +1 |
| 594 | Longest Harmonious Subsequence | Easy | Array, Hash Table, Counting +2 |
| 628 | Maximum Product of Three Numbers | Easy | Array, Math, Sorting |
| 645 | Set Mismatch | Easy | Bit Manipulation, Array, Hash Table +1 |
| 747 | Largest Number At Least Twice of Others | Easy | Array, Sorting |
| 888 | Fair Candy Swap | Easy | Array, Hash Table, Binary Search +1 |
| 905 | Sort Array By Parity | Easy | Array, Two Pointers, Sorting |
| 922 | Sort Array By Parity II | Easy | Array, Two Pointers, Sorting |
| 976 | Largest Perimeter Triangle | Easy | Greedy, Array, Math +1 |
| 977 | Squares of a Sorted Array | Easy | Array, Two Pointers, Sorting |
| 1005 | Maximize Sum Of Array After K Negations | Easy | Greedy, Array, Sorting |
| 1030 | Matrix Cells in Distance Order | Easy | Geometry, Array, Math +2 |
| 1051 | Height Checker | Easy | Array, Counting Sort, Sorting |
| 1122 | Relative Sort Array | Easy | Array, Hash Table, Counting Sort +1 |
| 1200 | Minimum Absolute Difference | Easy | Array, Sorting |
| 1331 | Rank Transform of an Array | Easy | Array, Hash Table, Sorting |
| 1337 | The K Weakest Rows in a Matrix | Easy | Array, Binary Search, Matrix +2 |
| 1346 | Check If N and Its Double Exist | Easy | Array, Hash Table, Two Pointers +2 |
| 1356 | Sort Integers by The Number of 1 Bits | Easy | Bit Manipulation, Array, Counting +1 |
| 1365 | How Many Numbers Are Smaller Than the Current Number | Easy | Array, Hash Table, Counting Sort +1 |
| 1385 | Find the Distance Value Between Two Arrays | Easy | Array, Two Pointers, Binary Search +1 |
| 1403 | Minimum Subsequence in Non-Increasing Order | Easy | Greedy, Array, Sorting |
| 1460 | Make Two Arrays Equal by Reversing Subarrays | Easy | Array, Hash Table, Sorting |
| 1464 | Maximum Product of Two Elements in an Array | Easy | Array, Sorting, Heap (Priority Queue) |
| 1491 | Average Salary Excluding the Minimum and Maximum Salary | Easy | Array, Sorting |
| 1502 | Can Make Arithmetic Progression From Sequence | Easy | Array, Sorting |
| 1608 | Special Array With X Elements Greater Than or Equal X | Easy | Array, Binary Search, Sorting |
| 1619 | Mean of Array After Removing Some Elements | Easy | Array, Sorting |
| 1636 | Sort Array by Increasing Frequency | Easy | Array, Hash Table, Sorting |
| 1637 | Widest Vertical Area Between Two Points Containing No Points | Easy | Array, Sorting |
| 1710 | Maximum Units on a Truck | Easy | Greedy, Array, Sorting |
| 1859 | Sorting the Sentence | Easy | String, Sorting |
Medium (118)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 15 | 3Sum | Medium | Array, Two Pointers, Sorting |
| 16 | 3Sum Closest | Medium | Array, Two Pointers, Sorting |
| 18 | 4Sum | Medium | Array, Two Pointers, Sorting |
| 47 | Permutations II | Medium | Array, Backtracking, Sorting |
| 49 | Group Anagrams | Medium | Array, Hash Table, String +1 |
| 56 | Merge Intervals | Medium | Array, Sorting |
| 75 | Sort Colors | Medium | Array, Two Pointers, Sorting |
| 147 | Insertion Sort List | Medium | Linked List, Sorting |
| 148 | Sort List | Medium | Linked List, Two Pointers, Divide and Conquer +2 |
| 164 | Maximum Gap | Medium | Array, Bucket Sort, Radix Sort +1 |
| 179 | Largest Number | Medium | Greedy, Array, String +1 |
| 215 | Kth Largest Element in an Array | Medium | Array, Divide and Conquer, Quickselect +2 |
| 229 | Majority Element II | Medium | Array, Hash Table, Counting +1 |
| 274 | H-Index | Medium | Array, Counting Sort, Sorting |
| 324 | Wiggle Sort II | Medium | Greedy, Array, Divide and Conquer +2 |
| 347 | Top K Frequent Elements | Medium | Array, Hash Table, Divide and Conquer +5 |
| 368 | Largest Divisible Subset | Medium | Array, Math, Dynamic Programming +1 |
| 378 | Kth Smallest Element in a Sorted Matrix | Medium | Array, Binary Search, Matrix +2 |
| 406 | Queue Reconstruction by Height | Medium | Binary Indexed Tree, Segment Tree, Array +1 |
| 435 | Non-overlapping Intervals | Medium | Greedy, Array, Dynamic Programming +1 |
| 436 | Find Right Interval | Medium | Array, Binary Search, Sorting |
| 442 | Find All Duplicates in an Array | Medium | Array, Hash Table, Sorting |
| 451 | Sort Characters By Frequency | Medium | Hash Table, String, Bucket Sort +3 |
| 452 | Minimum Number of Arrows to Burst Balloons | Medium | Greedy, Array, Sorting |
| 462 | Minimum Moves to Equal Array Elements II | Medium | Array, Math, Sorting |
| 475 | Heaters | Medium | Array, Two Pointers, Binary Search +1 |
| 522 | Longest Uncommon Subsequence II | Medium | Array, Hash Table, Two Pointers +2 |
| 524 | Longest Word in Dictionary through Deleting | Medium | Array, Two Pointers, String +1 |
| 532 | K-diff Pairs in an Array | Medium | Array, Hash Table, Two Pointers +2 |
| 539 | Minimum Time Difference | Medium | Array, Math, String +1 |
| 581 | Shortest Unsorted Continuous Subarray | Medium | Stack, Greedy, Array +3 |
| 611 | Valid Triangle Number | Medium | Greedy, Array, Two Pointers +2 |
| 621 | Task Scheduler | Medium | Greedy, Array, Hash Table +3 |
| 646 | Maximum Length of Pair Chain | Medium | Greedy, Array, Dynamic Programming +1 |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 692 | Top K Frequent Words | Medium | Trie, Array, Hash Table +5 |
| 720 | Longest Word in Dictionary | Medium | Trie, Array, Hash Table +2 |
| 721 | Accounts Merge | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 767 | Reorganize String | Medium | Greedy, Hash Table, String +3 |
| 769 | Max Chunks To Make Sorted | Medium | Stack, Greedy, Array +2 |
| 786 | K-th Smallest Prime Fraction | Medium | Array, Two Pointers, Binary Search +2 |
| 791 | Custom Sort String | Medium | Hash Table, String, Sorting |
| 792 | Number of Matching Subsequences | Medium | Trie, Array, Hash Table +4 |
| 823 | Binary Trees With Factors | Medium | Array, Hash Table, Dynamic Programming +1 |
| 825 | Friends Of Appropriate Ages | Medium | Array, Two Pointers, Binary Search +1 |
| 826 | Most Profit Assigning Work | Medium | Greedy, Array, Two Pointers +2 |
| 833 | Find And Replace in String | Medium | Array, Hash Table, String +1 |
| 846 | Hand of Straights | Medium | Greedy, Array, Hash Table +1 |
| 853 | Car Fleet | Medium | Stack, Array, Sorting +1 |
| 869 | Reordered Power of 2 | Medium | Hash Table, Math, Counting +2 |
| 870 | Advantage Shuffle | Medium | Greedy, Array, Two Pointers +1 |
| 881 | Boats to Save People | Medium | Greedy, Array, Two Pointers +1 |
| 893 | Groups of Special-Equivalent Strings | Medium | Array, Hash Table, String +1 |
| 910 | Smallest Range II | Medium | Greedy, Array, Math +1 |
| 912 | Sort an Array | Medium | Array, Divide and Conquer, Bucket Sort +5 |
| 923 | 3Sum With Multiplicity | Medium | Array, Hash Table, Two Pointers +2 |
| 937 | Reorder Data in Log Files | Medium | Array, String, Sorting |
| 939 | Minimum Area Rectangle | Medium | Geometry, Array, Hash Table +2 |
| 945 | Minimum Increment to Make Array Unique | Medium | Greedy, Array, Counting +1 |
| 948 | Bag of Tokens | Medium | Greedy, Array, Two Pointers +1 |
| 950 | Reveal Cards In Increasing Order | Medium | Queue, Array, Sorting +1 |
| 954 | Array of Doubled Pairs | Medium | Greedy, Array, Hash Table +1 |
| 969 | Pancake Sorting | Medium | Greedy, Array, Two Pointers +1 |
| 973 | K Closest Points to Origin | Medium | Geometry, Array, Math +4 |
| 1029 | Two City Scheduling | Medium | Greedy, Array, Sorting |
| 1040 | Moving Stones Until Consecutive II | Medium | Array, Math, Sorting +1 |
| 1048 | Longest String Chain | Medium | Array, Hash Table, Two Pointers +3 |
| 1054 | Distant Barcodes | Medium | Greedy, Array, Hash Table +3 |
| 1090 | Largest Values From Labels | Medium | Greedy, Array, Hash Table +2 |
| 1094 | Car Pooling | Medium | Array, Prefix Sum, Sorting +2 |
| 1169 | Invalid Transactions | Medium | Array, Hash Table, String +1 |
| 1170 | Compare Strings by Frequency of the Smallest Character | Medium | Array, Hash Table, String +2 |
| 1202 | Smallest String With Swaps | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1262 | Greatest Sum Divisible by Three | Medium | Greedy, Array, Dynamic Programming +1 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +3 |
| 1288 | Remove Covered Intervals | Medium | Array, Sorting |
| 1296 | Divide Array in Sets of K Consecutive Numbers | Medium | Greedy, Array, Hash Table +1 |
| 1300 | Sum of Mutated Array Closest to Target | Medium | Array, Binary Search, Sorting |
| 1305 | All Elements in Two Binary Search Trees | Medium | Tree, Depth-First Search, Binary Search Tree +2 |
| 1311 | Get Watched Videos by Your Friends | Medium | Breadth-First Search, Graph, Array +2 |
| 1329 | Sort the Matrix Diagonally | Medium | Array, Matrix, Sorting |
| 1333 | Filter Restaurants by Vegan-Friendly, Price and Distance | Medium | Array, Sorting |
| 1338 | Reduce Array Size to The Half | Medium | Greedy, Array, Hash Table +2 |
| 1348 | Tweet Counts Per Frequency | Medium | Design, Hash Table, String +3 |
| 1353 | Maximum Number of Events That Can Be Attended | Medium | Greedy, Array, Sorting +1 |
| 1366 | Rank Teams by Votes | Medium | Array, Hash Table, String +2 |
| 1387 | Sort Integers by The Power Value | Medium | Memoization, Dynamic Programming, Sorting |
| 1418 | Display Table of Food Orders in a Restaurant | Medium | Array, Hash Table, String +2 |
| 1424 | Diagonal Traverse II | Medium | Array, Sorting, Heap (Priority Queue) |
| 1433 | Check If a String Can Break Another String | Medium | Greedy, String, Sorting |
| 1451 | Rearrange Words in a Sentence | Medium | String, Sorting |
| 1465 | Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | Medium | Greedy, Array, Sorting |
| 1471 | The k Strongest Values in an Array | Medium | Array, Two Pointers, Sorting |
| 1481 | Least Number of Unique Integers after K Removals | Medium | Greedy, Array, Hash Table +2 |
| 1498 | Number of Subsequences That Satisfy the Given Sum Condition | Medium | Array, Two Pointers, Binary Search +1 |
| 1508 | Range Sum of Sorted Subarray Sums | Medium | Array, Two Pointers, Binary Search +2 |
| 1509 | Minimum Difference Between Largest and Smallest Value in Three Moves | Medium | Greedy, Array, Sorting |
| 1552 | Magnetic Force Between Two Balls | Medium | Array, Binary Search, Sorting |
| 1561 | Maximum Number of Coins You Can Get | Medium | Greedy, Array, Math +2 |
| 1589 | Maximum Sum Obtained of Any Permutation | Medium | Greedy, Array, Prefix Sum +1 |
| 1604 | Alert Using Same Key-Card Three or More Times in a One Hour Period | Medium | Array, Hash Table, String +1 |
| 1626 | Best Team With No Conflicts | Medium | Array, Dynamic Programming, Sorting |
| 1630 | Arithmetic Subarrays | Medium | Array, Hash Table, Sorting |
| 1647 | Minimum Deletions to Make Character Frequencies Unique | Medium | Greedy, Hash Table, String +1 |
| 1648 | Sell Diminishing-Valued Colored Balls | Medium | Greedy, Array, Math +3 |
| 1657 | Determine if Two Strings Are Close | Medium | Hash Table, String, Counting +1 |
| 1679 | Max Number of K-Sum Pairs | Medium | Array, Hash Table, Two Pointers +1 |
| 1686 | Stone Game VI | Medium | Greedy, Array, Math +3 |
| 1727 | Largest Submatrix With Rearrangements | Medium | Greedy, Array, Matrix +1 |
| 1738 | Find Kth Largest XOR Coordinate Value | Medium | Bit Manipulation, Array, Divide and Conquer +5 |
| 1798 | Maximum Number of Consecutive Values You Can Make | Medium | Greedy, Array, Sorting |
| 1818 | Minimum Absolute Sum Difference | Medium | Array, Binary Search, Ordered Set +1 |
| 1833 | Maximum Ice Cream Bars | Medium | Greedy, Array, Counting Sort +1 |
| 1834 | Single-Threaded CPU | Medium | Array, Sorting, Heap (Priority Queue) |
| 1838 | Frequency of the Most Frequent Element | Medium | Greedy, Array, Binary Search +3 |
| 1846 | Maximum Element After Decreasing and Rearranging | Medium | Greedy, Array, Sorting |
| 1877 | Minimize Maximum Pair Sum in Array | Medium | Greedy, Array, Two Pointers +1 |
| 1878 | Get Biggest Three Rhombus Sums in a Grid | Medium | Array, Math, Matrix +3 |
Hard (39)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 218 | The Skyline Problem | Hard | Binary Indexed Tree, Segment Tree, Array +5 |
| 220 | Contains Duplicate III | Hard | Array, Bucket Sort, Ordered Set +2 |
| 295 | Find Median from Data Stream | Hard | Design, Two Pointers, Data Stream +2 |
| 354 | Russian Doll Envelopes | Hard | Array, Binary Search, Dynamic Programming +1 |
| 472 | Concatenated Words | Hard | Depth-First Search, Trie, Array +3 |
| 502 | IPO | Hard | Greedy, Array, Sorting +1 |
| 630 | Course Schedule III | Hard | Greedy, Array, Sorting +1 |
| 632 | Smallest Range Covering Elements from K Lists | Hard | Greedy, Array, Hash Table +3 |
| 710 | Random Pick with Blacklist | Hard | Array, Hash Table, Math +3 |
| 719 | Find K-th Smallest Pair Distance | Hard | Array, Two Pointers, Binary Search +1 |
| 757 | Set Intersection Size At Least Two | Hard | Greedy, Array, Sorting |
| 768 | Max Chunks To Make Sorted II | Hard | Stack, Greedy, Array +2 |
| 857 | Minimum Cost to Hire K Workers | Hard | Greedy, Array, Sorting +1 |
| 891 | Sum of Subsequence Widths | Hard | Array, Math, Sorting |
| 899 | Orderly Queue | Hard | Math, String, Sorting |
| 975 | Odd Even Jump | Hard | Stack, Array, Dynamic Programming +3 |
| 987 | Vertical Order Traversal of a Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 1096 | Brace Expansion II | Hard | Stack, Breadth-First Search, Hash Table +3 |
| 1187 | Make Array Strictly Increasing | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1340 | Jump Game V | Hard | Array, Dynamic Programming, Sorting |
| 1363 | Largest Multiple of Three | Hard | Greedy, Array, Math +2 |
| 1383 | Maximum Performance of a Team | Hard | Greedy, Array, Sorting +1 |
| 1402 | Reducing Dishes | Hard | Greedy, Array, Dynamic Programming +1 |
| 1478 | Allocate Mailboxes | Hard | Array, Math, Dynamic Programming +1 |
| 1489 | Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree | Hard | Union Find, Graph, Minimum Spanning Tree +2 |
| 1547 | Minimum Cost to Cut a Stick | Hard | Array, Dynamic Programming, Sorting |
| 1585 | Check If String Is Transformable With Substring Sort Operations | Hard | Greedy, String, Sorting |
| 1610 | Maximum Number of Visible Points | Hard | Geometry, Array, Math +2 |
| 1632 | Rank Transform of a Matrix | Hard | Union Find, Graph, Topological Sort +3 |
| 1665 | Minimum Initial Energy to Finish Tasks | Hard | Greedy, Array, Sorting |
| 1691 | Maximum Height by Stacking Cuboids | Hard | Array, Dynamic Programming, Sorting |
| 1697 | Checking Existence of Edge Length Limited Paths | Hard | Union Find, Graph, Array +2 |
| 1751 | Maximum Number of Events That Can Be Attended II | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1755 | Closest Subsequence Sum | Hard | Bit Manipulation, Array, Two Pointers +3 |
| 1782 | Count Pairs Of Nodes | Hard | Graph, Array, Hash Table +4 |
| 1840 | Maximum Building Height | Hard | Array, Math, Sorting |
| 1847 | Closest Room | Hard | Array, Binary Search, Ordered Set +1 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Sorting problem arrives live
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.