Sorting LeetCode Problems: All 392,
With Python Solutions

Every problem in this library that LeetCode tags Sorting. 339 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
80
Medium
226
Hard
86
1 pattern

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.

Eyes only

When the Sorting problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

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)

Easy problems
No.Problem
0088Merge Sorted Array
0169Majority Element
0217Contains Duplicate
0242Valid Anagram
0268Missing Number
0349Intersection of Two Arrays
0350Intersection of Two Arrays II
0389Find the Difference
0414Third Maximum Number
0455Assign Cookies
0506Relative Ranks
0561Array Partition
0594Longest Harmonious Subsequence
0628Maximum Product of Three Numbers
0645Set Mismatch
0747Largest Number At Least Twice of Others
0888Fair Candy Swap
0905Sort Array By Parity
0922Sort Array By Parity II
0976Largest Perimeter Triangle
0977Squares of a Sorted Array
1005Maximize Sum Of Array After K Negations
1030Matrix Cells in Distance Order
1051Height Checker
1122Relative Sort Array
1200Minimum Absolute Difference
1331Rank Transform of an Array
1337The K Weakest Rows in a Matrix
1346Check If N and Its Double Exist
1356Sort Integers by The Number of 1 Bits
1365How Many Numbers Are Smaller Than the Current Number
1385Find the Distance Value Between Two Arrays
1403Minimum Subsequence in Non-Increasing Order
1460Make Two Arrays Equal by Reversing Subarrays
1464Maximum Product of Two Elements in an Array
1491Average Salary Excluding the Minimum and Maximum Salary
1502Can Make Arithmetic Progression From Sequence
1608Special Array With X Elements Greater Than or Equal X
1619Mean of Array After Removing Some Elements
1636Sort Array by Increasing Frequency
1637Widest Vertical Area Between Two Points Containing No Points
1710Maximum Units on a Truck
1859Sorting the Sentence

Medium (118)

Medium problems
No.Problem
00153Sum
00163Sum Closest
00184Sum
0047Permutations II
0049Group Anagrams
0056Merge Intervals
0075Sort Colors
0147Insertion Sort List
0148Sort List
0164Maximum Gap
0179Largest Number
0215Kth Largest Element in an Array
0229Majority Element II
0274H-Index
0324Wiggle Sort II
0347Top K Frequent Elements
0368Largest Divisible Subset
0378Kth Smallest Element in a Sorted Matrix
0406Queue Reconstruction by Height
0435Non-overlapping Intervals
0436Find Right Interval
0442Find All Duplicates in an Array
0451Sort Characters By Frequency
0452Minimum Number of Arrows to Burst Balloons
0462Minimum Moves to Equal Array Elements II
0475Heaters
0522Longest Uncommon Subsequence II
0524Longest Word in Dictionary through Deleting
0532K-diff Pairs in an Array
0539Minimum Time Difference
0581Shortest Unsorted Continuous Subarray
0611Valid Triangle Number
0621Task Scheduler
0646Maximum Length of Pair Chain
0658Find K Closest Elements
0692Top K Frequent Words
0720Longest Word in Dictionary
0721Accounts Merge
0767Reorganize String
0769Max Chunks To Make Sorted
0786K-th Smallest Prime Fraction
0791Custom Sort String
0792Number of Matching Subsequences
0823Binary Trees With Factors
0825Friends Of Appropriate Ages
0826Most Profit Assigning Work
0833Find And Replace in String
0846Hand of Straights
0853Car Fleet
0869Reordered Power of 2
0870Advantage Shuffle
0881Boats to Save People
0893Groups of Special-Equivalent Strings
0910Smallest Range II
0912Sort an Array
09233Sum With Multiplicity
0937Reorder Data in Log Files
0939Minimum Area Rectangle
0945Minimum Increment to Make Array Unique
0948Bag of Tokens
0950Reveal Cards In Increasing Order
0954Array of Doubled Pairs
0969Pancake Sorting
0973K Closest Points to Origin
1029Two City Scheduling
1040Moving Stones Until Consecutive II
1048Longest String Chain
1054Distant Barcodes
1090Largest Values From Labels
1094Car Pooling
1169Invalid Transactions
1170Compare Strings by Frequency of the Smallest Character
1202Smallest String With Swaps
1262Greatest Sum Divisible by Three
1268Search Suggestions System
1288Remove Covered Intervals
1296Divide Array in Sets of K Consecutive Numbers
1300Sum of Mutated Array Closest to Target
1305All Elements in Two Binary Search Trees
1311Get Watched Videos by Your Friends
1329Sort the Matrix Diagonally
1333Filter Restaurants by Vegan-Friendly, Price and Distance
1338Reduce Array Size to The Half
1348Tweet Counts Per Frequency
1353Maximum Number of Events That Can Be Attended
1366Rank Teams by Votes
1387Sort Integers by The Power Value
1418Display Table of Food Orders in a Restaurant
1424Diagonal Traverse II
1433Check If a String Can Break Another String
1451Rearrange Words in a Sentence
1465Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
1471The k Strongest Values in an Array
1481Least Number of Unique Integers after K Removals
1498Number of Subsequences That Satisfy the Given Sum Condition
1508Range Sum of Sorted Subarray Sums
1509Minimum Difference Between Largest and Smallest Value in Three Moves
1552Magnetic Force Between Two Balls
1561Maximum Number of Coins You Can Get
1589Maximum Sum Obtained of Any Permutation
1604Alert Using Same Key-Card Three or More Times in a One Hour Period
1626Best Team With No Conflicts
1630Arithmetic Subarrays
1647Minimum Deletions to Make Character Frequencies Unique
1648Sell Diminishing-Valued Colored Balls
1657Determine if Two Strings Are Close
1679Max Number of K-Sum Pairs
1686Stone Game VI
1727Largest Submatrix With Rearrangements
1738Find Kth Largest XOR Coordinate Value
1798Maximum Number of Consecutive Values You Can Make
1818Minimum Absolute Sum Difference
1833Maximum Ice Cream Bars
1834Single-Threaded CPU
1838Frequency of the Most Frequent Element
1846Maximum Element After Decreasing and Rearranging
1877Minimize Maximum Pair Sum in Array
1878Get Biggest Three Rhombus Sums in a Grid

Hard (39)

Hard problems
No.Problem
0218The Skyline Problem
0220Contains Duplicate III
0295Find Median from Data Stream
0354Russian Doll Envelopes
0472Concatenated Words
0502IPO
0630Course Schedule III
0632Smallest Range Covering Elements from K Lists
0710Random Pick with Blacklist
0719Find K-th Smallest Pair Distance
0757Set Intersection Size At Least Two
0768Max Chunks To Make Sorted II
0857Minimum Cost to Hire K Workers
0891Sum of Subsequence Widths
0899Orderly Queue
0975Odd Even Jump
0987Vertical Order Traversal of a Binary Tree
1096Brace Expansion II
1187Make Array Strictly Increasing
1235Maximum Profit in Job Scheduling
1340Jump Game V
1363Largest Multiple of Three
1383Maximum Performance of a Team
1402Reducing Dishes
1478Allocate Mailboxes
1489Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
1547Minimum Cost to Cut a Stick
1585Check If String Is Transformable With Substring Sort Operations
1610Maximum Number of Visible Points
1632Rank Transform of a Matrix
1665Minimum Initial Energy to Finish Tasks
1691Maximum Height by Stacking Cuboids
1697Checking Existence of Edge Length Limited Paths
1751Maximum Number of Events That Can Be Attended II
1755Closest Subsequence Sum
1782Count Pairs Of Nodes
1840Maximum Building Height
1847Closest Room
1851Minimum Interval to Include Each Query