LeetCode ยท Greedy

Greedy Problem Set

Make the locally optimal choice at every step. Sort + single pass + a few variables = greedy. Prove correctness with the exchange argument.

Concept page: Greedy
DifficultyPatternProblemKey Insight
MediumreachabilityLC 55 ยท Jump GameTrack farthest reachable index. O(n).
MediumBFS-greedyLC 45 ยท Jump Game IIBFS levels: track current range end and farthest. O(n).
MediumsubarrayLC 53 ยท Maximum SubarrayKadane's algorithm: reset running sum when negative. O(n).
MediumsurplusLC 134 ยท Gas StationRunning tank + total check. Reset on negative. O(n).
Hardtwo-passLC 135 ยท CandyLeft-to-right and right-to-left passes enforce both neighbor constraints. O(n).
MediumparenthesesLC 678 ยท Valid Parenthesis StringTrack min and max open count. O(n).
MediumpartitionLC 763 ยท Partition LabelsTrack last occurrence of each char. Extend partition greedily. O(n).
MediumgroupingLC 846 ยท Hand of StraightsSort + greedily form consecutive groups. O(n log n).
MediummergeLC 1899 ยท Merge Triplets to Form Target TripletFilter valid triplets, check if all target values are coverable. O(n).

โ† Back to all LeetCode categories