Greedy LeetCode Problems: All 346,
With Python Solutions

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

Easy
41
Medium
234
Hard
71
1 pattern

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.

Eyes only

When the Greedy 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.

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)

Easy problems
No.Problem
0409Longest Palindrome
0455Assign Cookies
0561Array Partition
0605Can Place Flowers
0680Valid Palindrome II
0860Lemonade Change
0942DI String Match
0976Largest Perimeter Triangle
1005Maximize Sum Of Array After K Negations
1013Partition Array Into Three Parts With Equal Sum
1217Minimum Cost to Move Chips to The Same Position
1221Split a String in Balanced Strings
1323Maximum 69 Number
1403Minimum Subsequence in Non-Increasing Order
1710Maximum Units on a Truck
1736Latest Time by Replacing Hidden Digits
1827Minimum Operations to Make the Array Increasing
1903Largest Odd Number in String
1974Minimum Time to Type Word Using Special Typewriter
2027Minimum Moves to Convert String
2037Minimum Number of Moves to Seat Everyone
2078Two Furthest Houses With Different Colors
2144Minimum Cost of Buying Candies With Discount
2160Minimum Sum of Four Digit Number After Splitting Digits

Medium (137)

Medium problems
No.Problem
0011Container With Most Water
0045Jump Game II
0055Jump Game
0122Best Time to Buy and Sell Stock II
0134Gas Station
0179Largest Number
0316Remove Duplicate Letters
0324Wiggle Sort II
0334Increasing Triplet Subsequence
0376Wiggle Subsequence
0397Integer Replacement
0402Remove K Digits
0435Non-overlapping Intervals
0452Minimum Number of Arrows to Burst Balloons
0581Shortest Unsorted Continuous Subarray
0611Valid Triangle Number
0621Task Scheduler
0624Maximum Distance in Arrays
0646Maximum Length of Pair Chain
0649Dota2 Senate
0659Split Array into Consecutive Subsequences
0670Maximum Swap
0678Valid Parenthesis String
0714Best Time to Buy and Sell Stock with Transaction Fee
0738Monotone Increasing Digits
0763Partition Labels
0767Reorganize String
0769Max Chunks To Make Sorted
0781Rabbits in Forest
0807Max Increase to Keep City Skyline
0826Most Profit Assigning Work
0846Hand of Straights
0861Score After Flipping Matrix
0870Advantage Shuffle
0881Boats to Save People
0910Smallest Range II
0921Minimum Add to Make Parentheses Valid
0945Minimum Increment to Make Array Unique
0948Bag of Tokens
0954Array of Doubled Pairs
0955Delete Columns to Make Sorted II
0969Pancake Sorting
0984String Without AAA or BBB
0991Broken Calculator
1007Minimum Domino Rotations For Equal Row
1024Video Stitching
1029Two City Scheduling
1053Previous Permutation With One Swap
1054Distant Barcodes
1081Smallest Subsequence of Distinct Characters
1090Largest Values From Labels
1130Minimum Cost Tree From Leaf Values
1144Decrease Elements To Make Array Zigzag
1247Minimum Swaps to Make Strings Equal
1253Reconstruct a 2-Row Binary Matrix
1262Greatest Sum Divisible by Three
1282Group the People Given the Group Size They Belong To
1296Divide Array in Sets of K Consecutive Numbers
1328Break a Palindrome
1338Reduce Array Size to The Half
1353Maximum Number of Events That Can Be Attended
1382Balance a Binary Search Tree
1386Cinema Seat Allocation
1400Construct K Palindrome Strings
1405Longest Happy String
1414Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
1432Max Difference You Can Get From Changing an Integer
1433Check If a String Can Break Another String
1465Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
1481Least Number of Unique Integers after K Removals
1488Avoid Flood in The City
1509Minimum Difference Between Largest and Smallest Value in Three Moves
1529Minimum Suffix Flips
1536Minimum Swaps to Arrange a Binary Grid
1541Minimum Insertions to Balance a Parentheses String
1546Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
1558Minimum Numbers of Function Calls to Make Target Array
1561Maximum Number of Coins You Can Get
1567Maximum Length of Subarray With Positive Product
1578Minimum Time to Make Rope Colorful
1589Maximum Sum Obtained of Any Permutation
1605Find Valid Matrix Given Row and Column Sums
1642Furthest Building You Can Reach
1647Minimum Deletions to Make Character Frequencies Unique
1648Sell Diminishing-Valued Colored Balls
1663Smallest String With A Given Numeric Value
1673Find the Most Competitive Subsequence
1686Stone Game VI
1689Partitioning Into Minimum Number Of Deci-Binary Numbers
1702Maximum Binary String After Change
1705Maximum Number of Eaten Apples
1717Maximum Score From Removing Substrings
1727Largest Submatrix With Rearrangements
1733Minimum Number of People to Teach
1753Maximum Score From Removing Stones
1754Largest Merge Of Two Strings
1764Form Array by Concatenating Subarrays of Another Array
1775Equal Sum Arrays With Minimum Number of Operations
1785Minimum Elements to Add to Form a Given Sum
1792Maximum Average Pass Ratio
1798Maximum Number of Consecutive Values You Can Make
1802Maximum Value at a Given Index in a Bounded Array
1824Minimum Sideway Jumps
1833Maximum Ice Cream Bars
1838Frequency of the Most Frequent Element
1846Maximum Element After Decreasing and Rearranging
1850Minimum Adjacent Swaps to Reach the Kth Smallest Number
1864Minimum Number of Swaps to Make the Binary String Alternating
1877Minimize Maximum Pair Sum in Array
1881Maximum Value after Insertion
1899Merge Triplets to Form Target Triplet
1921Eliminate Maximum Number of Monsters
1927Sum Game
1936Add Minimum Number of Rungs
1946Largest Number After Mutating Substring
1953Maximum Number of Weeks for Which You Can Work
1962Remove Stones to Minimize the Total
1963Minimum Number of Swaps to Make the String Balanced
1968Array With Elements Not Equal to Average of Neighbors
1969Minimum Non-Zero Product of the Array Elements
1975Maximum Matrix Sum
1996The Number of Weak Characters in the Game
2007Find Original Array From Doubled Array
2029Stone Game IX
2038Remove Colored Pieces if Both Neighbors are the Same Color
2064Minimized Maximum of Products Distributed to Any Store
2086Minimum Number of Food Buckets to Feed the Hamsters
2087Minimum Cost Homecoming of a Robot in a Grid
2091Removing Minimum and Maximum From Array
2116Check if a Parentheses String Can Be Valid
2126Destroying Asteroids
2131Longest Palindrome by Concatenating Two Letter Words
2139Minimum Moves to Reach Target Score
2170Minimum Operations to Make the Array Alternating
2171Removing Minimum Number of Magic Beans
2178Maximum Split of Positive Even Integers
2182Construct String With Repeat Limit

Hard (39)

Hard problems
No.Problem
0044Wildcard Matching
0135Candy
0321Create Maximum Number
0330Patching Array
0410Split Array Largest Sum
0420Strong Password Checker
0502IPO
0517Super Washing Machines
0630Course Schedule III
0632Smallest Range Covering Elements from K Lists
0757Set Intersection Size At Least Two
0765Couples Holding Hands
0768Max Chunks To Make Sorted II
0857Minimum Cost to Hire K Workers
0871Minimum Number of Refueling Stops
0936Stamping The Sequence
1147Longest Chunked Palindrome Decomposition
1326Minimum Number of Taps to Open to Water a Garden
1330Reverse Subarray To Maximize Array Value
1363Largest Multiple of Three
1383Maximum Performance of a Team
1388Pizza With 3n Slices
1402Reducing Dishes
1505Minimum Possible Integer After at Most K Adjacent Swaps On Digits
1526Minimum Number of Increments on Subarrays to Form a Target Array
1537Get the Maximum Score
1585Check If String Is Transformable With Substring Sort Operations
1665Minimum Initial Energy to Finish Tasks
1671Minimum Number of Removals to Make Mountain Array
1675Minimize Deviation in Array
1703Minimum Adjacent Swaps for K Consecutive Ones
1713Minimum Operations to Make a Subsequence
1739Building Boxes
2014Longest Subsequence Repeated k Times
2071Maximum Number of Tasks You Can Assign
2132Stamping the Grid
2136Earliest Possible Day of Full Bloom
2141Maximum Running Time of N Computers
2193Minimum Number of Moves to Make Palindrome