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)

#ProblemDifficultyTopics
409Longest PalindromeEasyGreedy, Hash Table, String
455Assign CookiesEasyGreedy, Array, Two Pointers +1
561Array PartitionEasyGreedy, Array, Counting Sort +1
605Can Place FlowersEasyGreedy, Array
680Valid Palindrome IIEasyGreedy, Two Pointers, String
860Lemonade ChangeEasyGreedy, Array
942DI String MatchEasyGreedy, Array, Two Pointers +1
976Largest Perimeter TriangleEasyGreedy, Array, Math +1
1005Maximize Sum Of Array After K NegationsEasyGreedy, Array, Sorting
1013Partition Array Into Three Parts With Equal SumEasyGreedy, Array
1217Minimum Cost to Move Chips to The Same PositionEasyGreedy, Array, Math
1221Split a String in Balanced StringsEasyGreedy, String, Counting
1323Maximum 69 NumberEasyGreedy, Math
1403Minimum Subsequence in Non-Increasing OrderEasyGreedy, Array, Sorting
1710Maximum Units on a TruckEasyGreedy, Array, Sorting
1736Latest Time by Replacing Hidden DigitsEasyGreedy, String
1827Minimum Operations to Make the Array IncreasingEasyGreedy, Array
1903Largest Odd Number in StringEasyGreedy, Math, String
1974Minimum Time to Type Word Using Special TypewriterEasyGreedy, String
2027Minimum Moves to Convert StringEasyGreedy, String
2037Minimum Number of Moves to Seat EveryoneEasyGreedy, Array, Counting Sort +1
2078Two Furthest Houses With Different ColorsEasyGreedy, Array
2144Minimum Cost of Buying Candies With DiscountEasyGreedy, Array, Sorting
2160Minimum Sum of Four Digit Number After Splitting DigitsEasyGreedy, Math, Sorting

Medium (137)

#ProblemDifficultyTopics
11Container With Most WaterMediumGreedy, Array, Two Pointers
45Jump Game IIMediumGreedy, Array, Dynamic Programming
55Jump GameMediumGreedy, Array, Dynamic Programming
122Best Time to Buy and Sell Stock IIMediumGreedy, Array, Dynamic Programming
134Gas StationMediumGreedy, Array
179Largest NumberMediumGreedy, Array, String +1
316Remove Duplicate LettersMediumStack, Greedy, String +1
324Wiggle Sort IIMediumGreedy, Array, Divide and Conquer +2
334Increasing Triplet SubsequenceMediumGreedy, Array
376Wiggle SubsequenceMediumGreedy, Array, Dynamic Programming
397Integer ReplacementMediumGreedy, Bit Manipulation, Memoization +1
402Remove K DigitsMediumStack, Greedy, String +1
435Non-overlapping IntervalsMediumGreedy, Array, Dynamic Programming +1
452Minimum Number of Arrows to Burst BalloonsMediumGreedy, Array, Sorting
581Shortest Unsorted Continuous SubarrayMediumStack, Greedy, Array +3
611Valid Triangle NumberMediumGreedy, Array, Two Pointers +2
621Task SchedulerMediumGreedy, Array, Hash Table +3
624Maximum Distance in ArraysMediumGreedy, Array
646Maximum Length of Pair ChainMediumGreedy, Array, Dynamic Programming +1
649Dota2 SenateMediumGreedy, Queue, String
659Split Array into Consecutive SubsequencesMediumGreedy, Array, Hash Table +1
670Maximum SwapMediumGreedy, Math
678Valid Parenthesis StringMediumStack, Greedy, String +1
714Best Time to Buy and Sell Stock with Transaction FeeMediumGreedy, Array, Dynamic Programming
738Monotone Increasing DigitsMediumGreedy, Math
763Partition LabelsMediumGreedy, Hash Table, Two Pointers +1
767Reorganize StringMediumGreedy, Hash Table, String +3
769Max Chunks To Make SortedMediumStack, Greedy, Array +2
781Rabbits in ForestMediumGreedy, Array, Hash Table +1
807Max Increase to Keep City SkylineMediumGreedy, Array, Matrix
826Most Profit Assigning WorkMediumGreedy, Array, Two Pointers +2
846Hand of StraightsMediumGreedy, Array, Hash Table +1
861Score After Flipping MatrixMediumGreedy, Bit Manipulation, Array +1
870Advantage ShuffleMediumGreedy, Array, Two Pointers +1
881Boats to Save PeopleMediumGreedy, Array, Two Pointers +1
910Smallest Range IIMediumGreedy, Array, Math +1
921Minimum Add to Make Parentheses ValidMediumStack, Greedy, String
945Minimum Increment to Make Array UniqueMediumGreedy, Array, Counting +1
948Bag of TokensMediumGreedy, Array, Two Pointers +1
954Array of Doubled PairsMediumGreedy, Array, Hash Table +1
955Delete Columns to Make Sorted IIMediumGreedy, Array, String
969Pancake SortingMediumGreedy, Array, Two Pointers +1
984String Without AAA or BBBMediumGreedy, String
991Broken CalculatorMediumGreedy, Math
1007Minimum Domino Rotations For Equal RowMediumGreedy, Array
1024Video StitchingMediumGreedy, Array, Dynamic Programming
1029Two City SchedulingMediumGreedy, Array, Sorting
1053Previous Permutation With One SwapMediumGreedy, Array
1054Distant BarcodesMediumGreedy, Array, Hash Table +3
1081Smallest Subsequence of Distinct CharactersMediumStack, Greedy, String +1
1090Largest Values From LabelsMediumGreedy, Array, Hash Table +2
1130Minimum Cost Tree From Leaf ValuesMediumStack, Greedy, Array +2
1144Decrease Elements To Make Array ZigzagMediumGreedy, Array
1247Minimum Swaps to Make Strings EqualMediumGreedy, Math, String
1253Reconstruct a 2-Row Binary MatrixMediumGreedy, Array, Matrix
1262Greatest Sum Divisible by ThreeMediumGreedy, Array, Dynamic Programming +1
1282Group the People Given the Group Size They Belong ToMediumGreedy, Array, Hash Table
1296Divide Array in Sets of K Consecutive NumbersMediumGreedy, Array, Hash Table +1
1328Break a PalindromeMediumGreedy, String
1338Reduce Array Size to The HalfMediumGreedy, Array, Hash Table +2
1353Maximum Number of Events That Can Be AttendedMediumGreedy, Array, Sorting +1
1382Balance a Binary Search TreeMediumGreedy, Tree, Depth-First Search +3
1386Cinema Seat AllocationMediumGreedy, Bit Manipulation, Array +1
1400Construct K Palindrome StringsMediumGreedy, Hash Table, String +1
1405Longest Happy StringMediumGreedy, String, Heap (Priority Queue)
1414Find the Minimum Number of Fibonacci Numbers Whose Sum Is KMediumGreedy, Math
1432Max Difference You Can Get From Changing an IntegerMediumGreedy, Math
1433Check If a String Can Break Another StringMediumGreedy, String, Sorting
1465Maximum Area of a Piece of Cake After Horizontal and Vertical CutsMediumGreedy, Array, Sorting
1481Least Number of Unique Integers after K RemovalsMediumGreedy, Array, Hash Table +2
1488Avoid Flood in The CityMediumGreedy, Array, Hash Table +2
1509Minimum Difference Between Largest and Smallest Value in Three MovesMediumGreedy, Array, Sorting
1529Minimum Suffix FlipsMediumGreedy, String
1536Minimum Swaps to Arrange a Binary GridMediumGreedy, Array, Matrix
1541Minimum Insertions to Balance a Parentheses StringMediumStack, Greedy, String
1546Maximum Number of Non-Overlapping Subarrays With Sum Equals TargetMediumGreedy, Array, Hash Table +1
1558Minimum Numbers of Function Calls to Make Target ArrayMediumGreedy, Bit Manipulation, Array
1561Maximum Number of Coins You Can GetMediumGreedy, Array, Math +2
1567Maximum Length of Subarray With Positive ProductMediumGreedy, Array, Dynamic Programming
1578Minimum Time to Make Rope ColorfulMediumGreedy, Array, String +1
1589Maximum Sum Obtained of Any PermutationMediumGreedy, Array, Prefix Sum +1
1605Find Valid Matrix Given Row and Column SumsMediumGreedy, Array, Matrix
1642Furthest Building You Can ReachMediumGreedy, Array, Heap (Priority Queue)
1647Minimum Deletions to Make Character Frequencies UniqueMediumGreedy, Hash Table, String +1
1648Sell Diminishing-Valued Colored BallsMediumGreedy, Array, Math +3
1663Smallest String With A Given Numeric ValueMediumGreedy, String
1673Find the Most Competitive SubsequenceMediumStack, Greedy, Array +1
1686Stone Game VIMediumGreedy, Array, Math +3
1689Partitioning Into Minimum Number Of Deci-Binary NumbersMediumGreedy, String
1702Maximum Binary String After ChangeMediumGreedy, String
1705Maximum Number of Eaten ApplesMediumGreedy, Array, Heap (Priority Queue)
1717Maximum Score From Removing SubstringsMediumStack, Greedy, String
1727Largest Submatrix With RearrangementsMediumGreedy, Array, Matrix +1
1733Minimum Number of People to TeachMediumGreedy, Array, Hash Table
1753Maximum Score From Removing StonesMediumGreedy, Math, Heap (Priority Queue)
1754Largest Merge Of Two StringsMediumGreedy, Two Pointers, String
1764Form Array by Concatenating Subarrays of Another ArrayMediumGreedy, Array, Two Pointers +1
1775Equal Sum Arrays With Minimum Number of OperationsMediumGreedy, Array, Hash Table +1
1785Minimum Elements to Add to Form a Given SumMediumGreedy, Array
1792Maximum Average Pass RatioMediumGreedy, Array, Heap (Priority Queue)
1798Maximum Number of Consecutive Values You Can MakeMediumGreedy, Array, Sorting
1802Maximum Value at a Given Index in a Bounded ArrayMediumGreedy, Math, Binary Search
1824Minimum Sideway JumpsMediumGreedy, Array, Dynamic Programming
1833Maximum Ice Cream BarsMediumGreedy, Array, Counting Sort +1
1838Frequency of the Most Frequent ElementMediumGreedy, Array, Binary Search +3
1846Maximum Element After Decreasing and RearrangingMediumGreedy, Array, Sorting
1850Minimum Adjacent Swaps to Reach the Kth Smallest NumberMediumGreedy, Two Pointers, String
1864Minimum Number of Swaps to Make the Binary String AlternatingMediumGreedy, String
1877Minimize Maximum Pair Sum in ArrayMediumGreedy, Array, Two Pointers +1
1881Maximum Value after InsertionMediumGreedy, String
1899Merge Triplets to Form Target TripletMediumGreedy, Array
1921Eliminate Maximum Number of MonstersMediumGreedy, Array, Sorting
1927Sum GameMediumGreedy, Math, String +1
1936Add Minimum Number of RungsMediumGreedy, Array
1946Largest Number After Mutating SubstringMediumGreedy, Array, String
1953Maximum Number of Weeks for Which You Can WorkMediumGreedy, Array
1962Remove Stones to Minimize the TotalMediumGreedy, Array, Heap (Priority Queue)
1963Minimum Number of Swaps to Make the String BalancedMediumStack, Greedy, Two Pointers +1
1968Array With Elements Not Equal to Average of NeighborsMediumGreedy, Array, Sorting
1969Minimum Non-Zero Product of the Array ElementsMediumGreedy, Recursion, Math
1975Maximum Matrix SumMediumGreedy, Array, Matrix
1996The Number of Weak Characters in the GameMediumStack, Greedy, Array +2
2007Find Original Array From Doubled ArrayMediumGreedy, Array, Hash Table +1
2029Stone Game IXMediumGreedy, Array, Math +2
2038Remove Colored Pieces if Both Neighbors are the Same ColorMediumGreedy, Math, String +1
2064Minimized Maximum of Products Distributed to Any StoreMediumGreedy, Array, Binary Search
2086Minimum Number of Food Buckets to Feed the HamstersMediumGreedy, String, Dynamic Programming
2087Minimum Cost Homecoming of a Robot in a GridMediumGreedy, Array
2091Removing Minimum and Maximum From ArrayMediumGreedy, Array
2116Check if a Parentheses String Can Be ValidMediumStack, Greedy, String
2126Destroying AsteroidsMediumGreedy, Array, Sorting
2131Longest Palindrome by Concatenating Two Letter WordsMediumGreedy, Array, Hash Table +2
2139Minimum Moves to Reach Target ScoreMediumGreedy, Math
2170Minimum Operations to Make the Array AlternatingMediumGreedy, Array, Hash Table +1
2171Removing Minimum Number of Magic BeansMediumGreedy, Array, Enumeration +2
2178Maximum Split of Positive Even IntegersMediumGreedy, Math, Backtracking
2182Construct String With Repeat LimitMediumGreedy, Hash Table, String +2

Hard (39)

#ProblemDifficultyTopics
44Wildcard MatchingHardGreedy, Recursion, String +1
135CandyHardGreedy, Array
321Create Maximum NumberHardStack, Greedy, Array +2
330Patching ArrayHardGreedy, Array
410Split Array Largest SumHardGreedy, Array, Binary Search +2
420Strong Password CheckerHardGreedy, String, Heap (Priority Queue)
502IPOHardGreedy, Array, Sorting +1
517Super Washing MachinesHardGreedy, Array
630Course Schedule IIIHardGreedy, Array, Sorting +1
632Smallest Range Covering Elements from K ListsHardGreedy, Array, Hash Table +3
757Set Intersection Size At Least TwoHardGreedy, Array, Sorting
765Couples Holding HandsHardGreedy, Depth-First Search, Breadth-First Search +2
768Max Chunks To Make Sorted IIHardStack, Greedy, Array +2
857Minimum Cost to Hire K WorkersHardGreedy, Array, Sorting +1
871Minimum Number of Refueling StopsHardGreedy, Array, Dynamic Programming +1
936Stamping The SequenceHardStack, Greedy, Queue +1
1147Longest Chunked Palindrome DecompositionHardGreedy, Two Pointers, String +3
1326Minimum Number of Taps to Open to Water a GardenHardGreedy, Array, Dynamic Programming
1330Reverse Subarray To Maximize Array ValueHardGreedy, Array, Math
1363Largest Multiple of ThreeHardGreedy, Array, Math +2
1383Maximum Performance of a TeamHardGreedy, Array, Sorting +1
1388Pizza With 3n SlicesHardGreedy, Array, Dynamic Programming +1
1402Reducing DishesHardGreedy, Array, Dynamic Programming +1
1505Minimum Possible Integer After at Most K Adjacent Swaps On DigitsHardGreedy, Binary Indexed Tree, Segment Tree +1
1526Minimum Number of Increments on Subarrays to Form a Target ArrayHardStack, Greedy, Array +2
1537Get the Maximum ScoreHardGreedy, Array, Two Pointers +1
1585Check If String Is Transformable With Substring Sort OperationsHardGreedy, String, Sorting
1665Minimum Initial Energy to Finish TasksHardGreedy, Array, Sorting
1671Minimum Number of Removals to Make Mountain ArrayHardGreedy, Array, Binary Search +1
1675Minimize Deviation in ArrayHardGreedy, Array, Ordered Set +1
1703Minimum Adjacent Swaps for K Consecutive OnesHardGreedy, Array, Prefix Sum +1
1713Minimum Operations to Make a SubsequenceHardGreedy, Array, Hash Table +1
1739Building BoxesHardGreedy, Math, Binary Search
2014Longest Subsequence Repeated k TimesHardGreedy, String, Backtracking +2
2071Maximum Number of Tasks You Can AssignHardGreedy, Queue, Array +4
2132Stamping the GridHardGreedy, Array, Matrix +1
2136Earliest Possible Day of Full BloomHardGreedy, Array, Sorting
2141Maximum Running Time of N ComputersHardGreedy, Array, Binary Search +1
2193Minimum Number of Moves to Make PalindromeHardGreedy, Binary Indexed Tree, Two Pointers +1

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.