Greedy Pattern: Template + 346 LeetCode Problems
Take the locally best option every time — when you can prove that never costs you later.
- 41 Easy
- 234 Medium
- 71 Hard
- O(n log n), dominated by the sort time
What the greedy pattern is
A greedy algorithm commits to the best-looking option at each step and never reconsiders, which makes it the fastest thing that can possibly work and also the easiest thing to get silently wrong. The hard part is never the code — it is three lines — but the justification, and the tool for that is the exchange argument: assume an optimal solution differs from the greedy one, look at the first place they diverge, and show that swapping in the greedy choice leaves the solution no worse. If that swap holds, greedy is correct; if it does not, the problem is dynamic programming wearing a disguise. Choosing what to be greedy about is usually a sorting decision, and the sort key is where the insight lives: interval scheduling works when you sort by earliest finishing time, because finishing early leaves the maximum room for everything after, and it quietly fails if you sort by start time or by duration instead.
When to use it
- A local choice can be shown, by an exchange argument, never to rule out an optimal completion.
- Sorting the input by one well-chosen key makes the right choice obvious at every step.
- The problem is interval scheduling, jump reachability, or an assignment with a clear ordering.
- A dynamic programming solution exists but its state collapses to a single running best.
The greedy 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 346 problems listed below.
def max_non_overlapping(intervals):
intervals.sort(key=lambda pair: pair[1]) # earliest finishing time first
last_end = float("-inf")
kept = 0
for start, finish in intervals:
if start >= last_end: # taking it never blocks a better option later
kept += 1
last_end = finish
return keptComplexity characteristics
- Time
- O(n log n), dominated by the sort
- Auxiliary space
- O(1) to O(n)
The greedy pass itself is a single linear scan; the sort that makes the greedy choice correct is what sets the complexity. When no sort is needed — a running maximum, a reachability check — the whole solution is O(n). The space is the sort's, which in Python means O(n) for Timsort's temporary buffer rather than the O(1) an in-place quicksort would suggest.
All 346 greedy LeetCode problems
Every problem in the library the greedy pattern applies to, grouped by LeetCode's own difficulty rating. 308 of the 346 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 (41)
Medium (234)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 11 | Container With Most Water | Medium | Greedy, Array, Two Pointers |
| 45 | Jump Game II | Medium | Greedy, Array, Dynamic Programming |
| 55 | Jump Game | Medium | Greedy, Array, Dynamic Programming |
| 122 | Best Time to Buy and Sell Stock II | Medium | Greedy, Array, Dynamic Programming |
| 134 | Gas Station | Medium | Greedy, Array |
| 179 | Largest Number | Medium | Greedy, Array, String +1 |
| 316 | Remove Duplicate Letters | Medium | Stack, Greedy, String +1 |
| 324 | Wiggle Sort II | Medium | Greedy, Array, Divide and Conquer +2 |
| 334 | Increasing Triplet Subsequence | Medium | Greedy, Array |
| 376 | Wiggle Subsequence | Medium | Greedy, Array, Dynamic Programming |
| 397 | Integer Replacement | Medium | Greedy, Bit Manipulation, Memoization +1 |
| 402 | Remove K Digits | Medium | Stack, Greedy, String +1 |
| 435 | Non-overlapping Intervals | Medium | Greedy, Array, Dynamic Programming +1 |
| 452 | Minimum Number of Arrows to Burst Balloons | Medium | Greedy, Array, Sorting |
| 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 |
| 624 | Maximum Distance in Arrays | Medium | Greedy, Array |
| 646 | Maximum Length of Pair Chain | Medium | Greedy, Array, Dynamic Programming +1 |
| 649 | Dota2 Senate | Medium | Greedy, Queue, String |
| 659 | Split Array into Consecutive Subsequences | Medium | Greedy, Array, Hash Table +1 |
| 670 | Maximum Swap | Medium | Greedy, Math |
| 678 | Valid Parenthesis String | Medium | Stack, Greedy, String +1 |
| 714 | Best Time to Buy and Sell Stock with Transaction Fee | Medium | Greedy, Array, Dynamic Programming |
| 738 | Monotone Increasing Digits | Medium | Greedy, Math |
| 763 | Partition Labels | Medium | Greedy, Hash Table, Two Pointers +1 |
| 846 | Hand of Straights | Medium | Greedy, Array, Hash Table +1 |
| 1899 | Merge Triplets to Form Target Triplet | Medium | Greedy, Array |
| 2542 | Maximum Subsequence Score | Medium | Greedy, Array, Sorting +1 |
| 253 | Meeting Rooms IIPremium | Medium | Greedy, Array, Two Pointers +3 |
| 280 | Wiggle SortPremium | Medium | Greedy, Array, Sorting |
| 484 | Find PermutationPremium | Medium | Stack, Greedy, Array +1 |
| 555 | Split Concatenated StringsPremium | Medium | Greedy, Array, String |
| 625 | Minimum FactorizationPremium | Medium | Greedy, Math |
| 767 | Reorganize String | Medium | Greedy, Hash Table, String +3 |
| 769 | Max Chunks To Make Sorted | Medium | Stack, Greedy, Array +2 |
| 781 | Rabbits in Forest | Medium | Greedy, Array, Hash Table +1 |
| 807 | Max Increase to Keep City Skyline | Medium | Greedy, Array, Matrix |
| 826 | Most Profit Assigning Work | Medium | Greedy, Array, Two Pointers +2 |
| 861 | Score After Flipping Matrix | Medium | Greedy, Bit Manipulation, Array +1 |
| 870 | Advantage Shuffle | Medium | Greedy, Array, Two Pointers +1 |
| 881 | Boats to Save People | Medium | Greedy, Array, Two Pointers +1 |
| 910 | Smallest Range II | Medium | Greedy, Array, Math +1 |
| 921 | Minimum Add to Make Parentheses Valid | Medium | Stack, Greedy, String |
| 945 | Minimum Increment to Make Array Unique | Medium | Greedy, Array, Counting +1 |
| 948 | Bag of Tokens | Medium | Greedy, Array, Two Pointers +1 |
| 954 | Array of Doubled Pairs | Medium | Greedy, Array, Hash Table +1 |
| 955 | Delete Columns to Make Sorted II | Medium | Greedy, Array, String |
| 969 | Pancake Sorting | Medium | Greedy, Array, Two Pointers +1 |
| 984 | String Without AAA or BBB | Medium | Greedy, String |
| 991 | Broken Calculator | Medium | Greedy, Math |
| 1007 | Minimum Domino Rotations For Equal Row | Medium | Greedy, Array |
| 1024 | Video Stitching | Medium | Greedy, Array, Dynamic Programming |
| 1029 | Two City Scheduling | Medium | Greedy, Array, Sorting |
| 1053 | Previous Permutation With One Swap | Medium | Greedy, Array |
| 1054 | Distant Barcodes | Medium | Greedy, Array, Hash Table +3 |
| 1055 | Shortest Way to Form StringPremium | Medium | Greedy, Two Pointers, String +1 |
| 1058 | Minimize Rounding Error to Meet TargetPremium | Medium | Greedy, Array, Math +2 |
| 1081 | Smallest Subsequence of Distinct Characters | Medium | Stack, Greedy, String +1 |
| 1090 | Largest Values From Labels | Medium | Greedy, Array, Hash Table +2 |
| 1130 | Minimum Cost Tree From Leaf Values | Medium | Stack, Greedy, Array +2 |
| 1144 | Decrease Elements To Make Array Zigzag | Medium | Greedy, Array |
| 1167 | Minimum Cost to Connect SticksPremium | Medium | Greedy, Array, Heap (Priority Queue) |
| 1247 | Minimum Swaps to Make Strings Equal | Medium | Greedy, Math, String |
| 1253 | Reconstruct a 2-Row Binary Matrix | Medium | Greedy, Array, Matrix |
| 1262 | Greatest Sum Divisible by Three | Medium | Greedy, Array, Dynamic Programming +1 |
| 1282 | Group the People Given the Group Size They Belong To | Medium | Greedy, Array, Hash Table |
| 1296 | Divide Array in Sets of K Consecutive Numbers | Medium | Greedy, Array, Hash Table +1 |
| 1328 | Break a Palindrome | Medium | Greedy, String |
| 1338 | Reduce Array Size to The Half | Medium | Greedy, Array, Hash Table +2 |
| 1353 | Maximum Number of Events That Can Be Attended | Medium | Greedy, Array, Sorting +1 |
| 1382 | Balance a Binary Search Tree | Medium | Greedy, Tree, Depth-First Search +3 |
| 1386 | Cinema Seat Allocation | Medium | Greedy, Bit Manipulation, Array +1 |
| 1400 | Construct K Palindrome Strings | Medium | Greedy, Hash Table, String +1 |
| 1405 | Longest Happy String | Medium | Greedy, String, Heap (Priority Queue) |
| 1414 | Find the Minimum Number of Fibonacci Numbers Whose Sum Is K | Medium | Greedy, Math |
| 1432 | Max Difference You Can Get From Changing an Integer | Medium | Greedy, Math |
| 1433 | Check If a String Can Break Another String | Medium | Greedy, String, Sorting |
| 1465 | Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | Medium | Greedy, Array, Sorting |
| 1481 | Least Number of Unique Integers after K Removals | Medium | Greedy, Array, Hash Table +2 |
| 1488 | Avoid Flood in The City | Medium | Greedy, Array, Hash Table +2 |
| 1509 | Minimum Difference Between Largest and Smallest Value in Three Moves | Medium | Greedy, Array, Sorting |
| 1529 | Minimum Suffix Flips | Medium | Greedy, String |
| 1536 | Minimum Swaps to Arrange a Binary Grid | Medium | Greedy, Array, Matrix |
| 1541 | Minimum Insertions to Balance a Parentheses String | Medium | Stack, Greedy, String |
| 1546 | Maximum Number of Non-Overlapping Subarrays With Sum Equals Target | Medium | Greedy, Array, Hash Table +1 |
| 1558 | Minimum Numbers of Function Calls to Make Target Array | Medium | Greedy, Bit Manipulation, Array |
| 1561 | Maximum Number of Coins You Can Get | Medium | Greedy, Array, Math +2 |
| 1564 | Put Boxes Into the Warehouse IPremium | Medium | Greedy, Array, Sorting |
| 1567 | Maximum Length of Subarray With Positive Product | Medium | Greedy, Array, Dynamic Programming |
| 1578 | Minimum Time to Make Rope Colorful | Medium | Greedy, Array, String +1 |
| 1580 | Put Boxes Into the Warehouse IIPremium | Medium | Greedy, Array, Sorting |
| 1589 | Maximum Sum Obtained of Any Permutation | Medium | Greedy, Array, Prefix Sum +1 |
| 1605 | Find Valid Matrix Given Row and Column Sums | Medium | Greedy, Array, Matrix |
| 1642 | Furthest Building You Can Reach | Medium | Greedy, Array, Heap (Priority Queue) |
| 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 |
| 1663 | Smallest String With A Given Numeric Value | Medium | Greedy, String |
| 1673 | Find the Most Competitive Subsequence | Medium | Stack, Greedy, Array +1 |
| 1686 | Stone Game VI | Medium | Greedy, Array, Math +3 |
| 1689 | Partitioning Into Minimum Number Of Deci-Binary Numbers | Medium | Greedy, String |
| 1702 | Maximum Binary String After Change | Medium | Greedy, String |
| 1705 | Maximum Number of Eaten Apples | Medium | Greedy, Array, Heap (Priority Queue) |
| 1717 | Maximum Score From Removing Substrings | Medium | Stack, Greedy, String |
| 1727 | Largest Submatrix With Rearrangements | Medium | Greedy, Array, Matrix +1 |
| 1733 | Minimum Number of People to Teach | Medium | Greedy, Array, Hash Table |
| 1753 | Maximum Score From Removing Stones | Medium | Greedy, Math, Heap (Priority Queue) |
| 1754 | Largest Merge Of Two Strings | Medium | Greedy, Two Pointers, String |
| 1764 | Form Array by Concatenating Subarrays of Another Array | Medium | Greedy, Array, Two Pointers +1 |
| 1775 | Equal Sum Arrays With Minimum Number of Operations | Medium | Greedy, Array, Hash Table +1 |
| 1785 | Minimum Elements to Add to Form a Given Sum | Medium | Greedy, Array |
| 1792 | Maximum Average Pass Ratio | Medium | Greedy, Array, Heap (Priority Queue) |
| 1794 | Count Pairs of Equal Substrings With Minimum DifferencePremium | Medium | Greedy, Hash Table, String |
| 1798 | Maximum Number of Consecutive Values You Can Make | Medium | Greedy, Array, Sorting |
| 1802 | Maximum Value at a Given Index in a Bounded Array | Medium | Greedy, Math, Binary Search |
| 1824 | Minimum Sideway Jumps | Medium | Greedy, Array, Dynamic Programming |
| 1833 | Maximum Ice Cream Bars | Medium | Greedy, Array, Counting Sort +1 |
| 1838 | Frequency of the Most Frequent Element | Medium | Greedy, Array, Binary Search +3 |
| 1846 | Maximum Element After Decreasing and Rearranging | Medium | Greedy, Array, Sorting |
| 1850 | Minimum Adjacent Swaps to Reach the Kth Smallest Number | Medium | Greedy, Two Pointers, String |
| 1864 | Minimum Number of Swaps to Make the Binary String Alternating | Medium | Greedy, String |
| 1874 | Minimize Product Sum of Two ArraysPremium | Medium | Greedy, Array, Sorting |
| 1877 | Minimize Maximum Pair Sum in Array | Medium | Greedy, Array, Two Pointers +1 |
| 1881 | Maximum Value after Insertion | Medium | Greedy, String |
| 1921 | Eliminate Maximum Number of Monsters | Medium | Greedy, Array, Sorting |
| 1927 | Sum Game | Medium | Greedy, Math, String +1 |
| 1936 | Add Minimum Number of Rungs | Medium | Greedy, Array |
| 1946 | Largest Number After Mutating Substring | Medium | Greedy, Array, String |
| 1953 | Maximum Number of Weeks for Which You Can Work | Medium | Greedy, Array |
| 1962 | Remove Stones to Minimize the Total | Medium | Greedy, Array, Heap (Priority Queue) |
| 1963 | Minimum Number of Swaps to Make the String Balanced | Medium | Stack, Greedy, Two Pointers +1 |
| 1968 | Array With Elements Not Equal to Average of Neighbors | Medium | Greedy, Array, Sorting |
| 1969 | Minimum Non-Zero Product of the Array Elements | Medium | Greedy, Recursion, Math |
| 1975 | Maximum Matrix Sum | Medium | Greedy, Array, Matrix |
| 1989 | Maximum Number of People That Can Be Caught in TagPremium | Medium | Greedy, Array |
| 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 |
| 2015 | Average Height of Buildings in Each SegmentPremium | Medium | Greedy, Array, Sorting +1 |
| 2029 | Stone Game IX | Medium | Greedy, Array, Math +2 |
| 2038 | Remove Colored Pieces if Both Neighbors are the Same Color | Medium | Greedy, Math, String +1 |
| 2064 | Minimized Maximum of Products Distributed to Any Store | Medium | Greedy, Array, Binary Search |
| 2086 | Minimum Number of Food Buckets to Feed the Hamsters | Medium | Greedy, String, Dynamic Programming |
| 2087 | Minimum Cost Homecoming of a Robot in a Grid | Medium | Greedy, Array |
| 2091 | Removing Minimum and Maximum From Array | Medium | Greedy, Array |
| 2098 | Subsequence of Size K With the Largest Even SumPremium | Medium | Greedy, Array, Sorting |
| 2116 | Check if a Parentheses String Can Be Valid | Medium | Stack, Greedy, String |
| 2126 | Destroying Asteroids | Medium | Greedy, Array, Sorting |
| 2131 | Longest Palindrome by Concatenating Two Letter Words | Medium | Greedy, Array, Hash Table +2 |
| 2139 | Minimum Moves to Reach Target Score | Medium | Greedy, Math |
| 2170 | Minimum Operations to Make the Array Alternating | Medium | Greedy, Array, Hash Table +1 |
| 2171 | Removing Minimum Number of Magic Beans | Medium | Greedy, Array, Enumeration +2 |
| 2178 | Maximum Split of Positive Even Integers | Medium | Greedy, Math, Backtracking |
| 2182 | Construct String With Repeat Limit | Medium | Greedy, Hash Table, String +2 |
| 2195 | Append K Integers With Minimal Sum | Medium | Greedy, Array, Math +1 |
| 2202 | Maximize the Topmost Element After K Moves | Medium | Greedy, Array |
| 2207 | Maximize Number of Subsequences in a String | Medium | Greedy, String, Prefix Sum |
| 2208 | Minimum Operations to Halve Array Sum | Medium | Greedy, Array, Heap (Priority Queue) |
| 2214 | Minimum Health to Beat GamePremium | Medium | Greedy, Array |
| 2216 | Minimum Deletions to Make Array Beautiful | Medium | Stack, Greedy, Array |
| 2233 | Maximum Product After K Increments | Medium | Greedy, Array, Heap (Priority Queue) |
| 2241 | Design an ATM Machine | Medium | Greedy, Design, Array |
| 2244 | Minimum Rounds to Complete All Tasks | Medium | Greedy, Array, Hash Table +1 |
| 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 |
| 2279 | Maximum Bags With Full Capacity of Rocks | Medium | Greedy, Array, Sorting |
| 2285 | Maximum Total Importance of Roads | Medium | Greedy, Graph, Sorting +1 |
| 2294 | Partition Array Such That Maximum Difference Is K | Medium | Greedy, Array, Sorting |
| 2310 | Sum of Numbers With Units Digit K | Medium | Greedy, Math, Dynamic Programming +1 |
| 2311 | Longest Binary Subsequence Less Than or Equal to K | Medium | Greedy, Memoization, String +1 |
| 2323 | Find Minimum Time to Finish All Jobs IIPremium | Medium | Greedy, Array, Sorting |
| 2333 | Minimum Sum of Squared Difference | Medium | Greedy, Array, Binary Search +2 |
| 2340 | Minimum Adjacent Swaps to Make a Valid ArrayPremium | Medium | Greedy, Array |
| 2358 | Maximum Number of Groups Entering a Competition | Medium | Greedy, Array, Math +1 |
| 2375 | Construct Smallest Number From DI String | Medium | Stack, Greedy, String +1 |
| 2384 | Largest Palindromic Number | Medium | Greedy, Hash Table, String +1 |
| 2405 | Optimal Partition of String | Medium | Greedy, Hash Table, String |
| 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 |
| 2422 | Merge Operations to Turn Array Into a PalindromePremium | Medium | Greedy, Array, Two Pointers |
| 2429 | Minimize XOR | Medium | Greedy, Bit Manipulation |
| 2434 | Using a Robot to Print the Lexicographically Smallest String | Medium | Stack, Greedy, Hash Table +1 |
| 2436 | Minimum Split Into Subarrays With GCD Greater Than OnePremium | Medium | Greedy, Array, Math +2 |
| 2439 | Minimize Maximum of Array | Medium | Greedy, Array, Binary Search +2 |
| 2457 | Minimum Addition to Make Integer Beautiful | Medium | Greedy, Math |
| 2486 | Append Characters to String to Make Subsequence | Medium | Greedy, Two Pointers, String |
| 2497 | Maximum Star Sum of a Graph | Medium | Greedy, Graph, Array +2 |
| 2498 | Frog Jump II | Medium | Greedy, Array, Binary Search |
| 2517 | Maximum Tastiness of Candy Basket | Medium | Greedy, Array, Binary Search +1 |
| 2522 | Partition String Into Substrings With Values at Most K | Medium | Greedy, String, Dynamic Programming |
| 2530 | Maximal Score After Applying K Operations | Medium | Greedy, Array, Heap (Priority Queue) |
| 2541 | Minimum Operations to Make Array Equal II | Medium | Greedy, Array, Math |
| 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 |
| 2560 | House Robber IV | Medium | Greedy, Array, Binary Search +1 |
| 2567 | Minimum Score by Changing Two Elements | Medium | Greedy, Array, Sorting |
| 2571 | Minimum Operations to Reduce an Integer to 0 | Medium | Greedy, Bit Manipulation, Dynamic Programming |
| 2576 | Find the Maximum Number of Marked Indices | Medium | Greedy, Array, Two Pointers +2 |
| 2587 | Rearrange Array to Maximize Prefix Score | Medium | Greedy, Array, Prefix Sum +1 |
| 2592 | Maximize Greatness of an Array | Medium | Greedy, Array, Two Pointers +1 |
| 2598 | Smallest Missing Non-negative Integer After Operations | Medium | Greedy, Array, Hash Table +1 |
| 2599 | Make the Prefix Sum Non-negativePremium | Medium | Greedy, Array, Heap (Priority Queue) |
| 2601 | Prime Subtraction Operation | Medium | Greedy, Array, Math +2 |
| 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 |
| 2645 | Minimum Additions to Make Valid String | Medium | Stack, Greedy, String +1 |
| 2673 | Make Costs of Paths Equal in a Binary Tree | Medium | Greedy, Tree, Array +2 |
| 2680 | Maximum OR | Medium | Greedy, Bit Manipulation, Array +1 |
| 2708 | Maximum Strength of a Group | Medium | Greedy, Bit Manipulation, Array +4 |
| 2712 | Minimum Cost to Make All Characters Equal | Medium | Greedy, String, Dynamic Programming |
| 2734 | Lexicographically Smallest String After Substring Operation | Medium | Greedy, String |
| 2745 | Construct the Longest New String | Medium | Greedy, Brainteaser, Math +1 |
| 2789 | Largest Element in an Array after Merge Operations | Medium | Greedy, Array |
| 2800 | Shortest String That Contains Three Strings | Medium | Greedy, String, Enumeration |
| 2811 | Check if it is Possible to Split Array | Medium | Greedy, Array, Dynamic Programming |
| 2829 | Determine the Minimum Sum of a k-avoiding Array | Medium | Greedy, Math |
| 2834 | Find the Minimum Possible Sum of a Beautiful Array | Medium | Greedy, Math |
| 2844 | Minimum Operations to Make a Special Number | Medium | Greedy, Math, String +1 |
| 2847 | Smallest Number With Given Digit ProductPremium | Medium | Greedy, Math |
| 2856 | Minimum Array Length After Pair Removals | Medium | Greedy, Array, Hash Table +3 |
| 2870 | Minimum Number of Operations to Make Array Empty | Medium | Greedy, Array, Hash Table +1 |
| 2871 | Split Array Into Maximum Number of Subarrays | Medium | Greedy, Bit Manipulation, Array |
| 2892 | Minimizing Array After Replacing Pairs With Their ProductPremium | Medium | Greedy, Array, Dynamic Programming |
| 2895 | Minimum Processing Time | Medium | Greedy, Array, Sorting |
| 2910 | Minimum Number of Groups to Create a Valid Assignment | Medium | Greedy, Array, Hash Table |
| 2918 | Minimum Equal Sum of Two Arrays After Replacing Zeros | Medium | Greedy, Array |
| 2938 | Separate Black and White Balls | Medium | Greedy, Two Pointers, String |
| 2939 | Maximum Xor Product | Medium | Greedy, Bit Manipulation, Math |
| 2952 | Minimum Number of Coins to be Added | Medium | Greedy, Array, Sorting |
| 2957 | Remove Adjacent Almost-Equal Characters | Medium | Greedy, String, Dynamic Programming |
| 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 (71)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 44 | Wildcard Matching | Hard | Greedy, Recursion, String +1 |
| 135 | Candy | Hard | Greedy, Array |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 330 | Patching Array | Hard | Greedy, Array |
| 410 | Split Array Largest Sum | Hard | Greedy, Array, Binary Search +2 |
| 420 | Strong Password Checker | Hard | Greedy, String, Heap (Priority Queue) |
| 502 | IPO | Hard | Greedy, Array, Sorting +1 |
| 517 | Super Washing Machines | Hard | Greedy, Array |
| 630 | Course Schedule III | Hard | Greedy, Array, Sorting +1 |
| 632 | Smallest Range Covering Elements from K Lists | Hard | Greedy, Array, Hash Table +3 |
| 358 | Rearrange String k Distance ApartPremium | Hard | Greedy, Hash Table, String +3 |
| 527 | Word AbbreviationPremium | Hard | Greedy, Trie, Array +2 |
| 757 | Set Intersection Size At Least Two | Hard | Greedy, Array, Sorting |
| 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 |
| 857 | Minimum Cost to Hire K Workers | Hard | Greedy, Array, Sorting +1 |
| 871 | Minimum Number of Refueling Stops | Hard | Greedy, Array, Dynamic Programming +1 |
| 936 | Stamping The Sequence | Hard | Stack, Greedy, Queue +1 |
| 1147 | Longest Chunked Palindrome Decomposition | Hard | Greedy, Two Pointers, String +3 |
| 1183 | Maximum Number of OnesPremium | Hard | Greedy, Math, Sorting +1 |
| 1199 | Minimum Time to Build BlocksPremium | Hard | Greedy, Array, Math +1 |
| 1326 | Minimum Number of Taps to Open to Water a Garden | Hard | Greedy, Array, Dynamic Programming |
| 1330 | Reverse Subarray To Maximize Array Value | Hard | Greedy, Array, Math |
| 1363 | Largest Multiple of Three | Hard | Greedy, Array, Math +2 |
| 1383 | Maximum Performance of a Team | Hard | Greedy, Array, Sorting +1 |
| 1388 | Pizza With 3n Slices | Hard | Greedy, Array, Dynamic Programming +1 |
| 1402 | Reducing Dishes | Hard | Greedy, Array, Dynamic Programming +1 |
| 1505 | Minimum Possible Integer After at Most K Adjacent Swaps On Digits | Hard | Greedy, Binary Indexed Tree, Segment Tree +1 |
| 1520 | Maximum Number of Non-Overlapping Substrings | Hard | Greedy, String |
| 1526 | Minimum Number of Increments on Subarrays to Form a Target Array | Hard | Stack, Greedy, Array +2 |
| 1537 | Get the Maximum Score | Hard | Greedy, Array, Two Pointers +1 |
| 1585 | Check If String Is Transformable With Substring Sort Operations | Hard | Greedy, String, Sorting |
| 1665 | Minimum Initial Energy to Finish Tasks | Hard | Greedy, Array, Sorting |
| 1671 | Minimum Number of Removals to Make Mountain Array | Hard | Greedy, Array, Binary Search +1 |
| 1675 | Minimize Deviation in Array | Hard | Greedy, Array, Ordered Set +1 |
| 1703 | Minimum Adjacent Swaps for K Consecutive Ones | Hard | Greedy, Array, Prefix Sum +1 |
| 1713 | Minimum Operations to Make a Subsequence | Hard | Greedy, Array, Hash Table +1 |
| 1739 | Building Boxes | Hard | Greedy, Math, Binary Search |
| 1788 | Maximize the Beauty of the GardenPremium | Hard | Greedy, Array, Hash Table +1 |
| 2014 | Longest Subsequence Repeated k Times | Hard | Greedy, String, Backtracking +2 |
| 2030 | Smallest K-Length Subsequence With Occurrences of a Letter | Hard | Stack, Greedy, String +1 |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2132 | Stamping the Grid | Hard | Greedy, Array, Matrix +1 |
| 2136 | Earliest Possible Day of Full Bloom | Hard | Greedy, Array, Sorting |
| 2141 | Maximum Running Time of N Computers | Hard | Greedy, Array, Binary Search +1 |
| 2193 | Minimum Number of Moves to Make Palindrome | Hard | Greedy, Binary Indexed Tree, Two Pointers +1 |
| 2234 | Maximum Total Beauty of the Gardens | Hard | Greedy, Array, Two Pointers +4 |
| 2263 | Make Array Non-decreasing or Non-increasingPremium | Hard | Greedy, Dynamic Programming |
| 2350 | Shortest Impossible Sequence of Rolls | Hard | Greedy, Array, Hash Table |
| 2366 | Minimum Replacements to Sort the Array | Hard | Greedy, Array, Math |
| 2412 | Minimum Money Required Before Transactions | Hard | Greedy, Array, Sorting |
| 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 |
| 2459 | Sort Array by Moving Items to Empty SpacePremium | Hard | Greedy, Array, Sorting |
| 2472 | Maximum Number of Non-overlapping Palindrome Substrings | Hard | Greedy, Two Pointers, String +1 |
| 2499 | Minimum Total Cost to Make Arrays Unequal | Hard | Greedy, Array, Hash Table +1 |
| 2528 | Maximize the Minimum Powered City | Hard | Greedy, Queue, Array +3 |
| 2551 | Put Marbles in Bags | Hard | Greedy, Array, Sorting +1 |
| 2561 | Rearranging Fruits | Hard | Greedy, Sort, Array +1 |
| 2573 | Find the String with LCP | Hard | Greedy, Union Find, Array +3 |
| 2589 | Minimum Time to Complete All Tasks | Hard | Stack, Greedy, Array +2 |
| 2659 | Make Array Empty | Hard | Greedy, Binary Indexed Tree, Segment Tree +4 |
| 2663 | Lexicographically Smallest Beautiful String | Hard | Greedy, String |
| 2790 | Maximum Number of Groups With Increasing Length | Hard | Greedy, Array, Math +2 |
| 2813 | Maximum Elegance of a K-Length Subsequence | Hard | Stack, Greedy, Array +3 |
| 2818 | Apply Operations to Maximize Score | Hard | Stack, Greedy, Array +4 |
| 2835 | Minimum Operations to Form Subsequence With Target Sum | Hard | Greedy, Bit Manipulation, Array |
| 2842 | Count K-Subsequences of a String With Maximum Beauty | Hard | Greedy, Hash Table, Math +2 |
| 2868 | The Wording GamePremium | Hard | Greedy, Array, Math +3 |
| 2897 | Apply Operations on Array to Maximize Sum of Squares | Hard | Greedy, Bit Manipulation, Array +1 |
| 2931 | Maximum Spending After Buying Items | Hard | Greedy, Array, Matrix +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.
Greedy pattern FAQ
What is the greedy pattern?
A greedy algorithm commits to the best-looking option at each step and never reconsiders, which makes it the fastest thing that can possibly work and also the easiest thing to get silently wrong.
How many LeetCode problems use the greedy pattern?
This page lists 346 LeetCode problems that the greedy pattern applies to: 41 Easy, 234 Medium and 71 Hard. 308 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the greedy pattern?
O(n log n), dominated by the sort time and O(1) to O(n) space. The greedy pass itself is a single linear scan; the sort that makes the greedy choice correct is what sets the complexity. When no sort is needed — a running maximum, a reachability check — the whole solution is O(n). The space is the sort's, which in Python means O(n) for Timsort's temporary buffer rather than the O(1) an in-place quicksort would suggest.
When should I use the greedy pattern in an interview?
A local choice can be shown, by an exchange argument, never to rule out an optimal completion. Sorting the input by one well-chosen key makes the right choice obvious at every step.
Which greedy problem should I start with?
LeetCode 409. Longest Palindrome 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 greedy?
Sorting, Dynamic Programming, Heap / Priority Queue, Two Pointers. 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 greedy 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.