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)

#ProblemDifficultyTopics
88Merge Sorted ArrayEasyArray, Two Pointers, Sorting
169Majority ElementEasyArray, Hash Table, Divide and Conquer +2
217Contains DuplicateEasyArray, Hash Table, Sorting
242Valid AnagramEasyHash Table, String, Sorting
268Missing NumberEasyBit Manipulation, Array, Hash Table +3
349Intersection of Two ArraysEasyArray, Hash Table, Two Pointers +2
350Intersection of Two Arrays IIEasyArray, Hash Table, Two Pointers +2
389Find the DifferenceEasyBit Manipulation, Hash Table, String +1
414Third Maximum NumberEasyArray, Sorting
455Assign CookiesEasyGreedy, Array, Two Pointers +1
506Relative RanksEasyArray, Sorting, Heap (Priority Queue)
561Array PartitionEasyGreedy, Array, Counting Sort +1
594Longest Harmonious SubsequenceEasyArray, Hash Table, Counting +2
628Maximum Product of Three NumbersEasyArray, Math, Sorting
645Set MismatchEasyBit Manipulation, Array, Hash Table +1
747Largest Number At Least Twice of OthersEasyArray, Sorting
888Fair Candy SwapEasyArray, Hash Table, Binary Search +1
905Sort Array By ParityEasyArray, Two Pointers, Sorting
922Sort Array By Parity IIEasyArray, Two Pointers, Sorting
976Largest Perimeter TriangleEasyGreedy, Array, Math +1
977Squares of a Sorted ArrayEasyArray, Two Pointers, Sorting
1005Maximize Sum Of Array After K NegationsEasyGreedy, Array, Sorting
1030Matrix Cells in Distance OrderEasyGeometry, Array, Math +2
1051Height CheckerEasyArray, Counting Sort, Sorting
1122Relative Sort ArrayEasyArray, Hash Table, Counting Sort +1
1200Minimum Absolute DifferenceEasyArray, Sorting
1331Rank Transform of an ArrayEasyArray, Hash Table, Sorting
1337The K Weakest Rows in a MatrixEasyArray, Binary Search, Matrix +2
1346Check If N and Its Double ExistEasyArray, Hash Table, Two Pointers +2
1356Sort Integers by The Number of 1 BitsEasyBit Manipulation, Array, Counting +1
1365How Many Numbers Are Smaller Than the Current NumberEasyArray, Hash Table, Counting Sort +1
1385Find the Distance Value Between Two ArraysEasyArray, Two Pointers, Binary Search +1
1403Minimum Subsequence in Non-Increasing OrderEasyGreedy, Array, Sorting
1460Make Two Arrays Equal by Reversing SubarraysEasyArray, Hash Table, Sorting
1464Maximum Product of Two Elements in an ArrayEasyArray, Sorting, Heap (Priority Queue)
1491Average Salary Excluding the Minimum and Maximum SalaryEasyArray, Sorting
1502Can Make Arithmetic Progression From SequenceEasyArray, Sorting
1608Special Array With X Elements Greater Than or Equal XEasyArray, Binary Search, Sorting
1619Mean of Array After Removing Some ElementsEasyArray, Sorting
1636Sort Array by Increasing FrequencyEasyArray, Hash Table, Sorting
1637Widest Vertical Area Between Two Points Containing No PointsEasyArray, Sorting
1710Maximum Units on a TruckEasyGreedy, Array, Sorting
1859Sorting the SentenceEasyString, Sorting

Medium (118)

#ProblemDifficultyTopics
153SumMediumArray, Two Pointers, Sorting
163Sum ClosestMediumArray, Two Pointers, Sorting
184SumMediumArray, Two Pointers, Sorting
47Permutations IIMediumArray, Backtracking, Sorting
49Group AnagramsMediumArray, Hash Table, String +1
56Merge IntervalsMediumArray, Sorting
75Sort ColorsMediumArray, Two Pointers, Sorting
147Insertion Sort ListMediumLinked List, Sorting
148Sort ListMediumLinked List, Two Pointers, Divide and Conquer +2
164Maximum GapMediumArray, Bucket Sort, Radix Sort +1
179Largest NumberMediumGreedy, Array, String +1
215Kth Largest Element in an ArrayMediumArray, Divide and Conquer, Quickselect +2
229Majority Element IIMediumArray, Hash Table, Counting +1
274H-IndexMediumArray, Counting Sort, Sorting
324Wiggle Sort IIMediumGreedy, Array, Divide and Conquer +2
347Top K Frequent ElementsMediumArray, Hash Table, Divide and Conquer +5
368Largest Divisible SubsetMediumArray, Math, Dynamic Programming +1
378Kth Smallest Element in a Sorted MatrixMediumArray, Binary Search, Matrix +2
406Queue Reconstruction by HeightMediumBinary Indexed Tree, Segment Tree, Array +1
435Non-overlapping IntervalsMediumGreedy, Array, Dynamic Programming +1
436Find Right IntervalMediumArray, Binary Search, Sorting
442Find All Duplicates in an ArrayMediumArray, Hash Table, Sorting
451Sort Characters By FrequencyMediumHash Table, String, Bucket Sort +3
452Minimum Number of Arrows to Burst BalloonsMediumGreedy, Array, Sorting
462Minimum Moves to Equal Array Elements IIMediumArray, Math, Sorting
475HeatersMediumArray, Two Pointers, Binary Search +1
522Longest Uncommon Subsequence IIMediumArray, Hash Table, Two Pointers +2
524Longest Word in Dictionary through DeletingMediumArray, Two Pointers, String +1
532K-diff Pairs in an ArrayMediumArray, Hash Table, Two Pointers +2
539Minimum Time DifferenceMediumArray, Math, String +1
581Shortest Unsorted Continuous SubarrayMediumStack, Greedy, Array +3
611Valid Triangle NumberMediumGreedy, Array, Two Pointers +2
621Task SchedulerMediumGreedy, Array, Hash Table +3
646Maximum Length of Pair ChainMediumGreedy, Array, Dynamic Programming +1
658Find K Closest ElementsMediumArray, Two Pointers, Binary Search +3
692Top K Frequent WordsMediumTrie, Array, Hash Table +5
720Longest Word in DictionaryMediumTrie, Array, Hash Table +2
721Accounts MergeMediumDepth-First Search, Breadth-First Search, Union Find +4
767Reorganize StringMediumGreedy, Hash Table, String +3
769Max Chunks To Make SortedMediumStack, Greedy, Array +2
786K-th Smallest Prime FractionMediumArray, Two Pointers, Binary Search +2
791Custom Sort StringMediumHash Table, String, Sorting
792Number of Matching SubsequencesMediumTrie, Array, Hash Table +4
823Binary Trees With FactorsMediumArray, Hash Table, Dynamic Programming +1
825Friends Of Appropriate AgesMediumArray, Two Pointers, Binary Search +1
826Most Profit Assigning WorkMediumGreedy, Array, Two Pointers +2
833Find And Replace in StringMediumArray, Hash Table, String +1
846Hand of StraightsMediumGreedy, Array, Hash Table +1
853Car FleetMediumStack, Array, Sorting +1
869Reordered Power of 2MediumHash Table, Math, Counting +2
870Advantage ShuffleMediumGreedy, Array, Two Pointers +1
881Boats to Save PeopleMediumGreedy, Array, Two Pointers +1
893Groups of Special-Equivalent StringsMediumArray, Hash Table, String +1
910Smallest Range IIMediumGreedy, Array, Math +1
912Sort an ArrayMediumArray, Divide and Conquer, Bucket Sort +5
9233Sum With MultiplicityMediumArray, Hash Table, Two Pointers +2
937Reorder Data in Log FilesMediumArray, String, Sorting
939Minimum Area RectangleMediumGeometry, Array, Hash Table +2
945Minimum Increment to Make Array UniqueMediumGreedy, Array, Counting +1
948Bag of TokensMediumGreedy, Array, Two Pointers +1
950Reveal Cards In Increasing OrderMediumQueue, Array, Sorting +1
954Array of Doubled PairsMediumGreedy, Array, Hash Table +1
969Pancake SortingMediumGreedy, Array, Two Pointers +1
973K Closest Points to OriginMediumGeometry, Array, Math +4
1029Two City SchedulingMediumGreedy, Array, Sorting
1040Moving Stones Until Consecutive IIMediumArray, Math, Sorting +1
1048Longest String ChainMediumArray, Hash Table, Two Pointers +3
1054Distant BarcodesMediumGreedy, Array, Hash Table +3
1090Largest Values From LabelsMediumGreedy, Array, Hash Table +2
1094Car PoolingMediumArray, Prefix Sum, Sorting +2
1169Invalid TransactionsMediumArray, Hash Table, String +1
1170Compare Strings by Frequency of the Smallest CharacterMediumArray, Hash Table, String +2
1202Smallest String With SwapsMediumDepth-First Search, Breadth-First Search, Union Find +4
1262Greatest Sum Divisible by ThreeMediumGreedy, Array, Dynamic Programming +1
1268Search Suggestions SystemMediumTrie, Array, String +3
1288Remove Covered IntervalsMediumArray, Sorting
1296Divide Array in Sets of K Consecutive NumbersMediumGreedy, Array, Hash Table +1
1300Sum of Mutated Array Closest to TargetMediumArray, Binary Search, Sorting
1305All Elements in Two Binary Search TreesMediumTree, Depth-First Search, Binary Search Tree +2
1311Get Watched Videos by Your FriendsMediumBreadth-First Search, Graph, Array +2
1329Sort the Matrix DiagonallyMediumArray, Matrix, Sorting
1333Filter Restaurants by Vegan-Friendly, Price and DistanceMediumArray, Sorting
1338Reduce Array Size to The HalfMediumGreedy, Array, Hash Table +2
1348Tweet Counts Per FrequencyMediumDesign, Hash Table, String +3
1353Maximum Number of Events That Can Be AttendedMediumGreedy, Array, Sorting +1
1366Rank Teams by VotesMediumArray, Hash Table, String +2
1387Sort Integers by The Power ValueMediumMemoization, Dynamic Programming, Sorting
1418Display Table of Food Orders in a RestaurantMediumArray, Hash Table, String +2
1424Diagonal Traverse IIMediumArray, Sorting, Heap (Priority Queue)
1433Check If a String Can Break Another StringMediumGreedy, String, Sorting
1451Rearrange Words in a SentenceMediumString, Sorting
1465Maximum Area of a Piece of Cake After Horizontal and Vertical CutsMediumGreedy, Array, Sorting
1471The k Strongest Values in an ArrayMediumArray, Two Pointers, Sorting
1481Least Number of Unique Integers after K RemovalsMediumGreedy, Array, Hash Table +2
1498Number of Subsequences That Satisfy the Given Sum ConditionMediumArray, Two Pointers, Binary Search +1
1508Range Sum of Sorted Subarray SumsMediumArray, Two Pointers, Binary Search +2
1509Minimum Difference Between Largest and Smallest Value in Three MovesMediumGreedy, Array, Sorting
1552Magnetic Force Between Two BallsMediumArray, Binary Search, Sorting
1561Maximum Number of Coins You Can GetMediumGreedy, Array, Math +2
1589Maximum Sum Obtained of Any PermutationMediumGreedy, Array, Prefix Sum +1
1604Alert Using Same Key-Card Three or More Times in a One Hour PeriodMediumArray, Hash Table, String +1
1626Best Team With No ConflictsMediumArray, Dynamic Programming, Sorting
1630Arithmetic SubarraysMediumArray, Hash Table, Sorting
1647Minimum Deletions to Make Character Frequencies UniqueMediumGreedy, Hash Table, String +1
1648Sell Diminishing-Valued Colored BallsMediumGreedy, Array, Math +3
1657Determine if Two Strings Are CloseMediumHash Table, String, Counting +1
1679Max Number of K-Sum PairsMediumArray, Hash Table, Two Pointers +1
1686Stone Game VIMediumGreedy, Array, Math +3
1727Largest Submatrix With RearrangementsMediumGreedy, Array, Matrix +1
1738Find Kth Largest XOR Coordinate ValueMediumBit Manipulation, Array, Divide and Conquer +5
1798Maximum Number of Consecutive Values You Can MakeMediumGreedy, Array, Sorting
1818Minimum Absolute Sum DifferenceMediumArray, Binary Search, Ordered Set +1
1833Maximum Ice Cream BarsMediumGreedy, Array, Counting Sort +1
1834Single-Threaded CPUMediumArray, Sorting, Heap (Priority Queue)
1838Frequency of the Most Frequent ElementMediumGreedy, Array, Binary Search +3
1846Maximum Element After Decreasing and RearrangingMediumGreedy, Array, Sorting
1877Minimize Maximum Pair Sum in ArrayMediumGreedy, Array, Two Pointers +1
1878Get Biggest Three Rhombus Sums in a GridMediumArray, Math, Matrix +3

Hard (39)

#ProblemDifficultyTopics
218The Skyline ProblemHardBinary Indexed Tree, Segment Tree, Array +5
220Contains Duplicate IIIHardArray, Bucket Sort, Ordered Set +2
295Find Median from Data StreamHardDesign, Two Pointers, Data Stream +2
354Russian Doll EnvelopesHardArray, Binary Search, Dynamic Programming +1
472Concatenated WordsHardDepth-First Search, Trie, Array +3
502IPOHardGreedy, Array, Sorting +1
630Course Schedule IIIHardGreedy, Array, Sorting +1
632Smallest Range Covering Elements from K ListsHardGreedy, Array, Hash Table +3
710Random Pick with BlacklistHardArray, Hash Table, Math +3
719Find K-th Smallest Pair DistanceHardArray, Two Pointers, Binary Search +1
757Set Intersection Size At Least TwoHardGreedy, Array, Sorting
768Max Chunks To Make Sorted IIHardStack, Greedy, Array +2
857Minimum Cost to Hire K WorkersHardGreedy, Array, Sorting +1
891Sum of Subsequence WidthsHardArray, Math, Sorting
899Orderly QueueHardMath, String, Sorting
975Odd Even JumpHardStack, Array, Dynamic Programming +3
987Vertical Order Traversal of a Binary TreeHardTree, Depth-First Search, Breadth-First Search +3
1096Brace Expansion IIHardStack, Breadth-First Search, Hash Table +3
1187Make Array Strictly IncreasingHardArray, Binary Search, Dynamic Programming +1
1235Maximum Profit in Job SchedulingHardArray, Binary Search, Dynamic Programming +1
1340Jump Game VHardArray, Dynamic Programming, Sorting
1363Largest Multiple of ThreeHardGreedy, Array, Math +2
1383Maximum Performance of a TeamHardGreedy, Array, Sorting +1
1402Reducing DishesHardGreedy, Array, Dynamic Programming +1
1478Allocate MailboxesHardArray, Math, Dynamic Programming +1
1489Find Critical and Pseudo-Critical Edges in Minimum Spanning TreeHardUnion Find, Graph, Minimum Spanning Tree +2
1547Minimum Cost to Cut a StickHardArray, Dynamic Programming, Sorting
1585Check If String Is Transformable With Substring Sort OperationsHardGreedy, String, Sorting
1610Maximum Number of Visible PointsHardGeometry, Array, Math +2
1632Rank Transform of a MatrixHardUnion Find, Graph, Topological Sort +3
1665Minimum Initial Energy to Finish TasksHardGreedy, Array, Sorting
1691Maximum Height by Stacking CuboidsHardArray, Dynamic Programming, Sorting
1697Checking Existence of Edge Length Limited PathsHardUnion Find, Graph, Array +2
1751Maximum Number of Events That Can Be Attended IIHardArray, Binary Search, Dynamic Programming +1
1755Closest Subsequence SumHardBit Manipulation, Array, Two Pointers +3
1782Count Pairs Of NodesHardGraph, Array, Hash Table +4
1840Maximum Building HeightHardArray, Math, Sorting
1847Closest RoomHardArray, Binary Search, Ordered Set +1
1851Minimum Interval to Include Each QueryHardArray, Binary Search, Sorting +2

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.