Greedy LeetCode Problems: All 346, With Python Solutions
Every problem in this library that LeetCode tags Greedy — 346 in total, 308 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 346 problems
- 41 Easy
- 234 Medium
- 71 Hard
How Greedy problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Greedy problems — each one explains the approach, gives a Python template and states its complexity.
- Greedy — Take the locally best option every time — when you can prove that never costs you later.
Greedy problems by difficulty
Showing the first 200 of 346 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (24)
Medium (137)
| # | 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 |
| 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 |
| 846 | Hand of Straights | Medium | Greedy, Array, Hash Table +1 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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) |
| 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 |
| 1877 | Minimize Maximum Pair Sum in Array | Medium | Greedy, Array, Two Pointers +1 |
| 1881 | Maximum Value after Insertion | Medium | Greedy, String |
| 1899 | Merge Triplets to Form Target Triplet | Medium | Greedy, Array |
| 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 |
| 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 |
| 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 |
| 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 |
Hard (39)
| # | 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 |
| 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 |
| 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 |
| 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 |
| 2014 | Longest Subsequence Repeated k Times | Hard | Greedy, String, Backtracking +2 |
| 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 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Greedy 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.