Sorting Pattern: Template + 401 LeetCode Problems
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial.
- 80 Easy
- 227 Medium
- 94 Hard
- O(n log n) time
What the sorting pattern is
Sorting is rarely the answer on its own; it is the setup that makes the answer obvious, and the skill being tested is choosing the key. Sorting intervals by start time puts every overlap next to its partner, so merging becomes one linear pass. Sorting by finishing time is what makes the greedy interval choice correct. Sorting an array before a two-pointer scan is what lets a comparison decide which pointer to move. Once the input is ordered, duplicates are adjacent, the median is at a known index, and binary search becomes available — three capabilities that between them dissolve a lot of problems. The cost is worth watching: O(n log n) is usually the cheap part next to whatever follows, but when the values are small integers, counting or bucket sort does it in linear time, and when only the kth element is wanted, quickselect gets it in expected linear time without sorting the rest. In Python the `key` argument, and tuples as keys for multi-level ordering, express nearly all of this without a comparator.
When to use it
- Adjacency in sorted order is what makes the answer visible: overlaps, duplicates, closest pairs.
- A greedy choice depends on processing items in a specific order.
- Grouping is by a canonical form that sorting produces, such as sorted letters for anagrams.
- Only the kth largest is needed and n is large — that is quickselect, not a full sort.
- Values are bounded small integers, where counting sort beats the comparison lower bound.
The sorting template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 401 problems listed below.
def merge_intervals(intervals):
intervals.sort(key=lambda pair: pair[0]) # sorting makes overlaps adjacent
merged = []
for start, finish in intervals:
if merged and start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], finish) # extend the open interval
else:
merged.append([start, finish])
return mergedComplexity characteristics
- Time
- O(n log n)
- Auxiliary space
- O(n) for Python's Timsort
Comparison sorts cannot beat n log n, and that bound is usually the cheap part next to whatever the sorted order then enables. Two escapes exist when the input allows them: counting, bucket and radix sort run in linear time when the values are bounded small integers, and quickselect finds the kth element in expected O(n) without ordering the rest. Python's sort is stable and allocates a temporary buffer, so it costs O(n) space rather than the O(log n) an in-place quicksort would suggest.
All 401 sorting LeetCode problems
Every problem in the library the sorting pattern applies to, grouped by LeetCode's own difficulty rating. 346 of the 401 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (80)
| # | 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 |
| 252 | Meeting RoomsPremium | 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 |
| 1065 | Index Pairs of a StringPremium | Easy | Trie, Array, String +1 |
| 1086 | High FivePremium | Easy | Array, Hash Table, Sorting +1 |
| 1099 | Two Sum Less Than KPremium | Easy | Array, Two Pointers, Binary Search +1 |
| 1122 | Relative Sort Array | Easy | Array, Hash Table, Counting Sort +1 |
| 1133 | Largest Unique NumberPremium | Easy | Array, Hash Table, Sorting |
| 1196 | How Many Apples Can You Put into the BasketPremium | Easy | Greedy, Array, Sorting |
| 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 |
| 1913 | Maximum Product Difference Between Two Pairs | Easy | Array, Sorting |
| 1984 | Minimum Difference Between Highest and Lowest of K Scores | Easy | Array, Sorting, Sliding Window |
| 2037 | Minimum Number of Moves to Seat Everyone | Easy | Greedy, Array, Counting Sort +1 |
| 2089 | Find Target Indices After Sorting Array | Easy | Array, Binary Search, Sorting |
| 2094 | Finding 3-Digit Even Numbers | Easy | Recursion, Array, Hash Table +2 |
| 2099 | Find Subsequence of Length K With the Largest Sum | Easy | Array, Hash Table, Sorting +1 |
| 2144 | Minimum Cost of Buying Candies With Discount | Easy | Greedy, Array, Sorting |
| 2148 | Count Elements With Strictly Smaller and Greater Elements | Easy | Array, Counting, Sorting |
| 2154 | Keep Multiplying Found Values by Two | Easy | Array, Hash Table, Sorting +1 |
| 2160 | Minimum Sum of Four Digit Number After Splitting Digits | Easy | Greedy, Math, Sorting |
| 2164 | Sort Even and Odd Indices Independently | Easy | Array, Sorting |
| 2229 | Check if an Array Is ConsecutivePremium | Easy | Array, Hash Table, Sorting |
| 2231 | Largest Number After Digit Swaps by Parity | Easy | Sorting, Heap (Priority Queue) |
| 2248 | Intersection of Multiple Arrays | Easy | Array, Hash Table, Counting +1 |
| 2273 | Find Resultant Array After Removing Anagrams | Easy | Array, Hash Table, String +1 |
| 2335 | Minimum Amount of Time to Fill Cups | Easy | Greedy, Array, Sorting +1 |
| 2357 | Make Array Zero by Subtracting Equal Amounts | Easy | Greedy, Array, Hash Table +3 |
| 2363 | Merge Similar Items | Easy | Array, Hash Table, Ordered Set +1 |
| 2389 | Longest Subsequence With Limited Sum | Easy | Greedy, Array, Binary Search +2 |
| 2418 | Sort the People | Easy | Array, Hash Table, String +1 |
| 2441 | Largest Positive Integer That Exists With Its Negative | Easy | Array, Hash Table, Two Pointers +1 |
| 2465 | Number of Distinct Averages | Easy | Array, Hash Table, Two Pointers +1 |
| 2475 | Number of Unequal Triplets in Array | Easy | Array, Hash Table, Sorting |
| 2500 | Delete Greatest Value in Each Row | Easy | Array, Matrix, Sorting +2 |
| 2578 | Split With Minimum Sum | Easy | Greedy, Math, Sorting |
| 2706 | Buy Two Chocolates | Easy | Greedy, Array, Sorting |
| 2733 | Neither Minimum nor Maximum | Easy | Array, Sorting |
| 2784 | Check if Array is Good | Easy | Array, Hash Table, Sorting |
| 2824 | Count Pairs Whose Sum is Less than Target | Easy | Array, Two Pointers, Binary Search +1 |
| 2974 | Minimum Number Game | Easy | Array, Sorting, Simulation +1 |
| 2996 | Smallest Missing Integer Greater Than Sequential Prefix Sum | Easy | Array, Hash Table, Sorting |
Medium (227)
| # | 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 |
| 846 | Hand of Straights | Medium | Greedy, Array, Hash Table +1 |
| 853 | Car Fleet | Medium | Stack, Array, Sorting +1 |
| 973 | K Closest Points to Origin | Medium | Geometry, Array, Math +4 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +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 |
| 2300 | Successful Pairs of Spells and Potions | Medium | Array, Two Pointers, Binary Search +1 |
| 2542 | Maximum Subsequence Score | Medium | Greedy, Array, Sorting +1 |
| 253 | Meeting Rooms IIPremium | Medium | Greedy, Array, Two Pointers +3 |
| 259 | 3Sum SmallerPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 280 | Wiggle SortPremium | Medium | Greedy, Array, Sorting |
| 314 | Binary Tree Vertical Order TraversalPremium | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 360 | Sort Transformed ArrayPremium | Medium | Array, Math, Two Pointers +1 |
| 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 |
| 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 |
| 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 |
| 1057 | Campus BikesPremium | Medium | Array, Sorting, Heap (Priority Queue) |
| 1058 | Minimize Rounding Error to Meet TargetPremium | Medium | Greedy, Array, Math +2 |
| 1087 | Brace ExpansionPremium | Medium | Stack, Breadth-First Search, String +2 |
| 1090 | Largest Values From Labels | Medium | Greedy, Array, Hash Table +2 |
| 1094 | Car Pooling | Medium | Array, Prefix Sum, Sorting +2 |
| 1101 | The Earliest Moment When Everyone Become FriendsPremium | Medium | Union Find, Array, Sorting |
| 1152 | Analyze User Website Visit PatternPremium | Medium | Array, Hash Table, String +1 |
| 1169 | Invalid Transactions | Medium | Array, Hash Table, String +1 |
| 1170 | Compare Strings by Frequency of the Smallest Character | Medium | Array, Hash Table, String +2 |
| 1181 | Before and After PuzzlePremium | Medium | Array, Hash Table, String +1 |
| 1202 | Smallest String With Swaps | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1229 | Meeting SchedulerPremium | Medium | Array, Two Pointers, Sorting |
| 1244 | Design A LeaderboardPremium | Medium | Design, Hash Table, Sorting |
| 1262 | Greatest Sum Divisible by Three | Medium | Greedy, Array, Dynamic Programming +1 |
| 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 |
| 1500 | Design a File Sharing SystemPremium | Medium | Design, Hash Table, Data Stream +2 |
| 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 |
| 1564 | Put Boxes Into the Warehouse IPremium | Medium | Greedy, Array, Sorting |
| 1580 | Put Boxes Into the Warehouse IIPremium | Medium | Greedy, Array, Sorting |
| 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 |
| 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 |
| 1772 | Sort Features by PopularityPremium | Medium | Array, Hash Table, String +1 |
| 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 |
| 1874 | Minimize Product Sum of Two ArraysPremium | 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 |
| 1885 | Count Pairs in Two ArraysPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 1887 | Reduction Operations to Make the Array Elements Equal | Medium | Array, Sorting |
| 1921 | Eliminate Maximum Number of Monsters | Medium | Greedy, Array, Sorting |
| 1943 | Describe the Painting | Medium | Array, Hash Table, Prefix Sum +1 |
| 1968 | Array With Elements Not Equal to Average of Neighbors | Medium | Greedy, Array, Sorting |
| 1985 | Find the Kth Largest Integer in the Array | Medium | Array, String, Divide and Conquer +3 |
| 1996 | The Number of Weak Characters in the Game | Medium | Stack, Greedy, Array +2 |
| 2007 | Find Original Array From Doubled Array | Medium | Greedy, Array, Hash Table +1 |
| 2008 | Maximum Earnings From Taxi | Medium | Array, Hash Table, Binary Search +2 |
| 2015 | Average Height of Buildings in Each SegmentPremium | Medium | Greedy, Array, Sorting +1 |
| 2021 | Brightest Position on StreetPremium | Medium | Array, Ordered Set, Prefix Sum +1 |
| 2031 | Count Subarrays With More Ones Than ZerosPremium | Medium | Binary Indexed Tree, Segment Tree, Array +5 |
| 2033 | Minimum Operations to Make a Uni-Value Grid | Medium | Array, Math, Matrix +1 |
| 2046 | Sort Linked List Already Sorted Using Absolute ValuesPremium | Medium | Linked List, Two Pointers, Sorting |
| 2054 | Two Best Non-Overlapping Events | Medium | Array, Binary Search, Dynamic Programming +2 |
| 2070 | Most Beautiful Item for Each Query | Medium | Array, Binary Search, Sorting |
| 2098 | Subsequence of Size K With the Largest Even SumPremium | Medium | Greedy, Array, Sorting |
| 2126 | Destroying Asteroids | Medium | Greedy, Array, Sorting |
| 2135 | Count Words Obtained After Adding a Letter | Medium | Bit Manipulation, Array, Hash Table +2 |
| 2146 | K Highest Ranked Items Within a Price Range | Medium | Breadth-First Search, Array, Matrix +2 |
| 2165 | Smallest Value of the Rearranged Number | Medium | Math, Sorting |
| 2171 | Removing Minimum Number of Magic Beans | Medium | Greedy, Array, Enumeration +2 |
| 2191 | Sort the Jumbled Numbers | Medium | Array, Sorting |
| 2195 | Append K Integers With Minimal Sum | Medium | Greedy, Array, Math +1 |
| 2225 | Find Players With Zero or One Losses | Medium | Array, Hash Table, Counting +1 |
| 2250 | Count Number of Rectangles Containing Each Point | Medium | Binary Indexed Tree, Array, Hash Table +2 |
| 2268 | Minimum Number of KeypressesPremium | Medium | Greedy, Hash Table, String +2 |
| 2271 | Maximum White Tiles Covered by a Carpet | Medium | Greedy, Array, Binary Search +3 |
| 2274 | Maximum Consecutive Floors Without Special Floors | Medium | Array, Sorting |
| 2279 | Maximum Bags With Full Capacity of Rocks | Medium | Greedy, Array, Sorting |
| 2280 | Minimum Lines to Represent a Line Chart | Medium | Geometry, Array, Math +2 |
| 2285 | Maximum Total Importance of Roads | Medium | Greedy, Graph, Sorting +1 |
| 2294 | Partition Array Such That Maximum Difference Is K | Medium | Greedy, Array, Sorting |
| 2323 | Find Minimum Time to Finish All Jobs IIPremium | Medium | Greedy, Array, Sorting |
| 2332 | The Latest Time to Catch a Bus | Medium | Array, Two Pointers, Binary Search +1 |
| 2333 | Minimum Sum of Squared Difference | Medium | Greedy, Array, Binary Search +2 |
| 2342 | Max Sum of a Pair With Equal Sum of Digits | Medium | Array, Hash Table, Sorting +1 |
| 2343 | Query Kth Smallest Trimmed Number | Medium | Array, String, Divide and Conquer +4 |
| 2345 | Finding the Number of Visible MountainsPremium | Medium | Stack, Array, Sorting +1 |
| 2406 | Divide Intervals Into Minimum Number of Groups | Medium | Greedy, Array, Two Pointers +3 |
| 2410 | Maximum Matching of Players With Trainers | Medium | Greedy, Array, Two Pointers +1 |
| 2456 | Most Popular Video Creator | Medium | Array, Hash Table, String +2 |
| 2491 | Divide Players Into Teams of Equal Skill | Medium | Array, Hash Table, Two Pointers +1 |
| 2497 | Maximum Star Sum of a Graph | Medium | Greedy, Graph, Array +2 |
| 2501 | Longest Square Streak in an Array | Medium | Array, Hash Table, Binary Search +2 |
| 2512 | Reward Top K Students | Medium | Array, Hash Table, String +2 |
| 2517 | Maximum Tastiness of Candy Basket | Medium | Greedy, Array, Binary Search +1 |
| 2545 | Sort the Students by Their Kth Score | Medium | Array, Matrix, Sorting |
| 2548 | Maximum Price to Fill a BagPremium | Medium | Greedy, Array, Sorting |
| 2554 | Maximum Number of Integers to Choose From a Range I | Medium | Greedy, Array, Hash Table +2 |
| 2557 | Maximum Number of Integers to Choose From a Range IIPremium | Medium | Greedy, Array, Binary Search +1 |
| 2563 | Count the Number of Fair Pairs | Medium | Array, Two Pointers, Binary Search +1 |
| 2567 | Minimum Score by Changing Two Elements | Medium | Greedy, Array, Sorting |
| 2576 | Find the Maximum Number of Marked Indices | Medium | Greedy, Array, Two Pointers +2 |
| 2580 | Count Ways to Group Overlapping Ranges | Medium | Array, Sorting |
| 2583 | Kth Largest Sum in a Binary Tree | Medium | Tree, Breadth-First Search, Binary Tree +1 |
| 2587 | Rearrange Array to Maximize Prefix Score | Medium | Greedy, Array, Prefix Sum +1 |
| 2590 | Design a Todo ListPremium | Medium | Design, Array, Hash Table +2 |
| 2592 | Maximize Greatness of an Array | Medium | Greedy, Array, Two Pointers +1 |
| 2593 | Find Score of an Array After Marking All Elements | Medium | Array, Hash Table, Sorting +2 |
| 2597 | The Number of Beautiful Subsets | Medium | Array, Hash Table, Math +4 |
| 2602 | Minimum Operations to Make All Array Elements Equal | Medium | Array, Binary Search, Prefix Sum +1 |
| 2607 | Make K-Subarray Sums Equal | Medium | Greedy, Array, Math +2 |
| 2611 | Mice and Cheese | Medium | Greedy, Array, Sorting +1 |
| 2616 | Minimize the Maximum Difference of Pairs | Medium | Greedy, Array, Binary Search +2 |
| 2638 | Count the Number of K-Free SubsetsPremium | Medium | Array, Math, Dynamic Programming +2 |
| 2655 | Find Maximal Uncovered RangesPremium | Medium | Array, Sorting |
| 2679 | Sum in a Matrix | Medium | Array, Matrix, Sorting +2 |
| 2708 | Maximum Strength of a Group | Medium | Greedy, Bit Manipulation, Array +4 |
| 2731 | Movement of Robots | Medium | Brainteaser, Array, Prefix Sum +1 |
| 2740 | Find the Value of the Partition | Medium | Array, Sorting |
| 2747 | Count Zero Request Servers | Medium | Array, Hash Table, Sorting +1 |
| 2766 | Relocate Marbles | Medium | Array, Hash Table, Sorting +1 |
| 2779 | Maximum Beauty of an Array After Applying Operation | Medium | Array, Binary Search, Sorting +1 |
| 2780 | Minimum Index of a Valid Split | Medium | Array, Hash Table, Sorting |
| 2785 | Sort Vowels in a String | Medium | String, Sorting |
| 2830 | Maximize the Profit as the Salesman | Medium | Array, Hash Table, Binary Search +2 |
| 2838 | Maximum Coins Heroes Can CollectPremium | Medium | Array, Two Pointers, Binary Search +2 |
| 2840 | Check if Strings Can be Made Equal With Operations II | Medium | Hash Table, String, Sorting |
| 2860 | Happy Students | Medium | Array, Enumeration, Sorting |
| 2863 | Maximum Length of Semi-Decreasing SubarraysPremium | Medium | Stack, Array, Sorting +1 |
| 2895 | Minimum Processing Time | Medium | Greedy, Array, Sorting |
| 2933 | High-Access Employees | Medium | Array, Hash Table, String +1 |
| 2943 | Maximize Area of Square Hole in Grid | Medium | Array, Sorting |
| 2948 | Make Lexicographically Smallest Array by Swapping Elements | Medium | Union Find, Array, Sorting |
| 2952 | Minimum Number of Coins to be Added | Medium | Greedy, Array, Sorting |
| 2966 | Divide Array Into Arrays With Max Difference | Medium | Greedy, Array, Sorting |
| 2967 | Minimum Cost to Make Array Equalindromic | Medium | Greedy, Array, Math +2 |
| 2971 | Find Polygon With the Largest Perimeter | Medium | Greedy, Array, Prefix Sum +1 |
Hard (94)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 23 | Merge k Sorted Lists | Hard | Linked List, Divide and Conquer, Heap (Priority Queue) +1 |
| 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 |
| 315 | Count of Smaller Numbers After Self | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 327 | Count of Range Sum | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 354 | Russian Doll Envelopes | Hard | Array, Binary Search, Dynamic Programming +1 |
| 472 | Concatenated Words | Hard | Depth-First Search, Trie, Array +3 |
| 493 | Reverse Pairs | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 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 |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
| 296 | Best Meeting PointPremium | Hard | Array, Math, Matrix +1 |
| 358 | Rearrange String k Distance ApartPremium | Hard | Greedy, Hash Table, String +3 |
| 527 | Word AbbreviationPremium | Hard | Greedy, Trie, Array +2 |
| 588 | Design In-Memory File SystemPremium | Hard | Design, Trie, Hash Table +2 |
| 642 | Design Search Autocomplete SystemPremium | Hard | Depth-First Search, Design, Trie +4 |
| 726 | Number of Atoms | Hard | Stack, Hash Table, String +1 |
| 757 | Set Intersection Size At Least Two | Hard | Greedy, Array, Sorting |
| 759 | Employee Free TimePremium | Hard | Array, Sorting, Line Sweep +1 |
| 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 |
| 1183 | Maximum Number of OnesPremium | Hard | Greedy, Math, Sorting +1 |
| 1187 | Make Array Strictly Increasing | 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 |
| 1649 | Create Sorted Array through Instructions | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 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 |
| 1889 | Minimum Space Wasted From Packaging | Hard | Array, Binary Search, Prefix Sum +1 |
| 1998 | GCD Sort of an Array | Hard | Union Find, Array, Math +2 |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2092 | Find All People With Secret | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 2122 | Recover the Original Array | Hard | Array, Hash Table, Two Pointers +2 |
| 2136 | Earliest Possible Day of Full Bloom | Hard | Greedy, Array, Sorting |
| 2141 | Maximum Running Time of N Computers | Hard | Greedy, Array, Binary Search +1 |
| 2179 | Count Good Triplets in an Array | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2234 | Maximum Total Beauty of the Gardens | Hard | Greedy, Array, Two Pointers +4 |
| 2242 | Maximum Score of a Node Sequence | Hard | Graph, Array, Enumeration +1 |
| 2251 | Number of Flowers in Full Bloom | Hard | Array, Hash Table, Binary Search +3 |
| 2344 | Minimum Deletions to Make Array Divisible | Hard | Array, Math, Number Theory +2 |
| 2371 | Minimize Maximum Value in a GridPremium | Hard | Union Find, Graph, Topological Sort +3 |
| 2386 | Find the K-Sum of an Array | Hard | Array, Sorting, Heap (Priority Queue) |
| 2402 | Meeting Rooms III | Hard | Array, Hash Table, Sorting +2 |
| 2412 | Minimum Money Required Before Transactions | Hard | Greedy, Array, Sorting |
| 2421 | Number of Good Paths | Hard | Tree, Union Find, Graph +3 |
| 2426 | Number of Pairs Satisfying Inequality | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2448 | Minimum Cost to Make Array Equal | Hard | Greedy, Array, Binary Search +2 |
| 2449 | Minimum Number of Operations to Make Arrays Similar | Hard | Greedy, Array, Sorting |
| 2454 | Next Greater Element IV | Hard | Stack, Array, Binary Search +3 |
| 2459 | Sort Array by Moving Items to Empty SpacePremium | Hard | Greedy, Array, Sorting |
| 2463 | Minimum Total Distance Traveled | Hard | Array, Dynamic Programming, Sorting |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2519 | Count the Number of K-Big IndicesPremium | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2551 | Put Marbles in Bags | Hard | Greedy, Array, Sorting +1 |
| 2589 | Minimum Time to Complete All Tasks | Hard | Stack, Greedy, Array +2 |
| 2604 | Minimum Time to Eat All GrainsPremium | Hard | Array, Two Pointers, Binary Search +1 |
| 2613 | Beautiful PairsPremium | Hard | Geometry, Array, Math +3 |
| 2659 | Make Array Empty | Hard | Greedy, Binary Indexed Tree, Segment Tree +4 |
| 2681 | Power of Heroes | Hard | Array, Math, Dynamic Programming +2 |
| 2713 | Maximum Strictly Increasing Cells in a Matrix | Hard | Memoization, Array, Hash Table +5 |
| 2736 | Maximum Sum Queries | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2751 | Robot Collisions | Hard | Stack, Array, Sorting +1 |
| 2790 | Maximum Number of Groups With Increasing Length | Hard | Greedy, Array, Math +2 |
| 2809 | Minimum Time to Make Array Sum At Most x | Hard | Array, Dynamic Programming, Sorting |
| 2813 | Maximum Elegance of a K-Length Subsequence | Hard | Stack, Greedy, Array +3 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 2819 | Minimum Relative Loss After Buying ChocolatesPremium | Hard | Array, Binary Search, Prefix Sum +1 |
| 2931 | Maximum Spending After Buying Items | Hard | Greedy, Array, Matrix +2 |
| 2968 | Apply Operations to Maximize Frequency Score | Hard | Array, Binary Search, Prefix Sum +2 |
| 2973 | Find Number of Coins to Place in Tree Nodes | Hard | Tree, Depth-First Search, Dynamic Programming +2 |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Sorting pattern FAQ
What is the sorting pattern?
Sorting is rarely the answer on its own; it is the setup that makes the answer obvious, and the skill being tested is choosing the key. Sorting intervals by start time puts every overlap next to its partner, so merging becomes one linear pass.
How many LeetCode problems use the sorting pattern?
This page lists 401 LeetCode problems that the sorting pattern applies to: 80 Easy, 227 Medium and 94 Hard. 346 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the sorting pattern?
O(n log n) time and O(n) for Python's Timsort space. Comparison sorts cannot beat n log n, and that bound is usually the cheap part next to whatever the sorted order then enables. Two escapes exist when the input allows them: counting, bucket and radix sort run in linear time when the values are bounded small integers, and quickselect finds the kth element in expected O(n) without ordering the rest. Python's sort is stable and allocates a temporary buffer, so it costs O(n) space rather than the O(log n) an in-place quicksort would suggest.
When should I use the sorting pattern in an interview?
Adjacency in sorted order is what makes the answer visible: overlaps, duplicates, closest pairs. A greedy choice depends on processing items in a specific order.
Which sorting problem should I start with?
LeetCode 88. Merge Sorted Array is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to sorting?
Greedy, Two Pointers, Binary Search, Heap / Priority Queue. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the sorting problem you did not practise
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.